title: Symfony2 LexikFormFilter bundle custom filter
body: <p>I have an entity named <strong><code>Product</code></strong> with a <code>name</code> property.</p>
  
  <p>For a product named <strong>"Red Light Saber"</strong> for example, I would like to be able to get it as a result when I type <em>"Saber"</em> or <em>"Red"</em> in the filter text field.</p>
  
  <p>But actually, I need to type exactly <em>"Red Light Saber"</em> for the product to appears.</p>
  
  <p>I'm using <a href="https://github.com/lexik/LexikFormFilterBundle/blob/master/Resources/doc/working-with-the-bundle.md" rel="nofollow">LexikFormFilterBundle</a>. To solve my problem, I try to do what the documentation explain in section <strong>iv. Filter customization</strong>.</p>
  
  <p>This my <code>ProductFilterType.php</code></p>
  
  <pre><code>&lt;?php
  
  namespace ProjectList\Bundle\ProductBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  use Doctrine\ORM\EntityRepository;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\Form\FormError;
  use Symfony\Component\Form\FormBuilder;
  use Doctrine\ORM\QueryBuilder;
  use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;
  
  class ProductFilterType extends AbstractType
  {
      public $choices;
  
      public function __construct($providers)
      {
          foreach ($providers as $provider) {
              $this-&gt;choices[$provider-&gt;getId()] = $provider-&gt;getName();
          }
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name', 'filter_text', array('apply_filter' =&gt; function (QueryInterface $filterQuery, $field, $values)
              {
                  if (empty($values['value'])) {
                      return null;
                  }
  
                  $paramName = sprintf('p_%s', str_replace('.', '_', $field));
  
                  // expression that represent the condition
                  $expression = $filterQuery-&gt;getExpr()-&gt;eq($field, ':' . $paramName);
                  var_dump($expression);
                  die;
  
                  // expression parameters
                  $parameters = array($paramName =&gt; $values['value']); // [ name =&gt; value ]
                  // or if you need to define the parameter's type
                  // $parameters = array($paramName =&gt; array($values['value'], \PDO::PARAM_STR)); // [ name =&gt; [value, type] ]
  
                  return $filterQuery-&gt;createCondition($expression, $parameters);
              },
              ))
              -&gt;add('description', 'filter_text')
              -&gt;add('type', 'filter_choice', array(
                  'choices' =&gt; array(0 =&gt; 'Gadget', 1 =&gt; 'Textile')
              ))
              -&gt;add('provider', 'filter_choice', array(
                  'choices' =&gt; $this-&gt;choices,
              ));
  
          $listener = function (FormEvent $event) {
              // Is data empty?
              foreach ($event-&gt;getData() as $data) {
                  if (is_array($data)) {
                      foreach ($data as $subData) {
                          if (!empty($subData)) return;
                      }
                  } else {
                      if (!empty($data)) return;
                  }
              }
  
              $event-&gt;getForm()-&gt;addError(new FormError('Filter empty'));
          };
          $builder-&gt;addEventListener(FormEvents::POST_BIND, $listener);
      }
  
      public function getName()
      {
          return 'projectlist_bundle_entitybundle_productfiltertype';
      }
  }
  </code></pre>
  
  <p>This is my var_dump($expression) of line 40 result :</p>
  
  <pre><code>object(Doctrine\ORM\Query\Expr\Comparison)[620]
    protected 'leftExpr' =&gt; string 'e.name' (length=6)
    protected 'operator' =&gt; string '=' (length=1)
    protected 'rightExpr' =&gt; string ':p_e_name' (length=9)
  </code></pre>
  
  <p>I think I have to change the operator for something like <code>contain</code> but I don't know how.</p>