Download the PHP package andanteproject/page-filter-form-bundle without Composer
On this page you can find all versions of the php package andanteproject/page-filter-form-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andanteproject/page-filter-form-bundle
More information about andanteproject/page-filter-form-bundle
Files in andanteproject/page-filter-form-bundle
Package page-filter-form-bundle
Short Description A Symfony Bundle to simplify the handling of page filters for lists/tables in admin panels.
License MIT
Informations about the package page-filter-form-bundle

Page Filter Form Bundle
Symfony Bundle - AndanteProject
A Symfony Bundle to simplify the handling of page filters for lists/tables in admin panels. ๐งช
Requirements
Symfony 4.x-7.x and PHP 7.4-8.0.
Features
- Use Symfony Form;
- Keep your URL parameters clean as
?search=value&otherFilterName=anotherValueby default; - Form will work even if you render form elements outside the form tag, around the web page, exactly where you need, avoiding nested form conflicts.
- Super easy to implement and maintain;
- Works like magic โจ.
How to install
After installation, make sure you have the bundle registered in your Symfony bundles list (config/bundles.php):
This should have been done automatically if you are using Symfony Flex. Otherwise, register it yourself.
The problem
Let's suppose you have this common admin panel controller with a page listing some Employee entities.
To add filters to this page, let's create a Symfony form.
Let's add this Form to our controller page:
The code above has some huge problems:
- ๐ Handling all this filter logic inside the controller is not a good idea. Sure, you can move it into a dedicated
service, but that means creating another class file alongside
EmployeeFilterTypeto handle filters, and that still does not solve the second point in this list; - ๐ You need to carry around and match form element names.
search,seniorandorderByare keys you could store in constants to avoid repeating yourself, but this will drive you crazy as the filter logic grows.
The solution with Page Filter Form
Use Andante\PageFilterFormBundle\Form\PageFilterType as parent of your filter
form (why?) and implement target_callback option on your form elements like
this:
Implement Andante\PageFilterFormBundle\PageFilterFormTrait in your controller (or inject
Andante\PageFilterFormBundle\PageFilterManagerInterface as an argument) and use the form like this:
โ Done!
- ๐ Controller is clean and easy to read;
- ๐ We have just one class taking care of filters;
- ๐ The option
target_callbacklets you avoid repeating yourself and carrying around form element names; - ๐ You can type-hint your callable ๐ฅฐ (check callback arguments);
- ๐ We got you covered solving possible nested form problems (how?);
"target_callback" option
target_callback
type: null or callable default: null
The callable is going to have 3 parameters (third is optional):
| Parameter | What | Mandatory | Description |
|---|---|---|---|
| 1 | Filter $target |
yes |
The second argument of createAndHandleFilter. It can be whatever you want: a query builder, an array, a collection, an object. It does not matter as long as you match its type with this argument signature. |
| 2 | form data | yes |
Equivalent to calling $form->getData() on the current form field. It will be a ?string for a TextType or a ?\DateTime for a DateTimeType. |
| 3 | form itself | no |
The current $form instance. |
Why use PageFilterType as form parent
You could avoid using Andante\PageFilterFormBundle\Form\PageFilterType as the parent of your form, but be aware it sets
some useful defaults you may want to replicate:
| Option | Value | Description |
|---|---|---|
method |
GET |
You probably want filters to be part of the page URL, right? |
csrf_protection |
false |
So users can share the page URL with others without running into problems. |
allow_extra_fields |
true |
Allows other URL parameters outside your form values. |
andante_smart_form_attr |
true |
Enables form elements to be rendered wherever you want on your page, even outside the form tag, while keeping them working properly (discover more). |
Render the form in twig
As long as andante_smart_form_attr is true, you can render your form like this:
โ
The form.perPage element works properly even outside the form tag (how?!).
Give us a โญ!
Built with love โค๏ธ by AndanteProject team.