Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Render only one input for many fields #194

Closed
pbrzoski opened this issue Jan 5, 2016 · 1 comment
Closed

Render only one input for many fields #194

pbrzoski opened this issue Jan 5, 2016 · 1 comment

Comments

@pbrzoski
Copy link

pbrzoski commented Jan 5, 2016

Hi
I have entity CustomerMachine.php

CustomerMachine {
    protected $name;
    protected $price;
    protected $type;
}

Now, I would like create searching using LexikFormFilterBuilder, so I create something like this.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', 'text', array());
    $builder->add('price', 'text', array());
    $builder->add('type', 'text', array());
}

And this works and is OK.

This renders 3 inputs in template. But I would like render only one field( search fields ), which will be create 'OR' condition on name, price, type columns. It is possible ?

@rvanlaak
Copy link
Contributor

rvanlaak commented Jan 6, 2016

Yes that's possible. You don't have to add the fields you currently have, but should ad a search field that isn't mapped to the entity, and has a custom apply_filter:

->add('search', TextFilterType::class, array(
    'apply_filter' => function (ORMQuery $filterBuilder, $field, $values) {
        if (!empty($values['value'])) {
            $filterBuilder->getQueryBuilder()
                ->orWhere("p.name LIKE '%:value%'")
                ->orWhere("p.price LIKE '%:value%'")
                ->orWhere("p.type LIKE '%:value%'")
                ->setParameter('value', $values['value'])
            ;
        }
    },
    'mapped' => false,
))

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants