Filters
The listings of the index page can be refined with
filters, a series of form controls that add conditions to the query
(e.g. price > 10, enabled = true).
Define your filters with the configureFilters() method of your
dashboard or CRUD controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
namespace App\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class ProductCrudController extends AbstractCrudController
{
// ...
public function configureFilters(Filters $filters): Filters
{
return $filters
->add('title')
->add('price')
->add('published')
;
}
}
EasyAdmin provides ready-to-use filters for the most common needs (dates, numeric values, collections, etc.). The type of filter is automatically selected based on the data type of the property, but you can also define the filter type explicitly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
namespace App\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
class ProductCrudController extends AbstractCrudController
{
// ...
public function configureFilters(Filters $filters): Filters
{
return $filters
->add('title')
->add('price')
// most of the time there is no need to define the
// filter type because EasyAdmin can guess it automatically
->add(BooleanFilter::new('published'))
;
}
}
Filters on Associated Properties
Filters can also be applied to the properties of associated entities and
Doctrine embeddables. Use the dot syntax used
by association fields (association.property) to traverse any number of
nested associations; EasyAdmin creates the needed Doctrine JOIN clauses
automatically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
namespace App\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\TextFilter;
class BookCrudController extends AbstractCrudController
{
// ...
public function configureFilters(Filters $filters): Filters
{
return $filters
// 'author' is an association of the Book entity
->add(TextFilter::new('author.fullName', 'Author Name'))
// 'address' is a Doctrine embeddable inside the Author entity
->add(TextFilter::new('author.address.country', 'Author Country'))
// EntityFilter paths must end in an association instead of a property
->add(EntityFilter::new('author.publisher'))
;
}
}
Built-in Filters
Common Filter Options
All built-in filters share these common configuration methods:
setLabel(TranslatableInterface|string|false|null $label): customizes or hides the filter labelsetFormType(string $formTypeFqcn): changes the form type used to render the filtersetFormTypeOption(string $name, mixed $value): sets a single form type optionsetFormTypeOptions(array $options): sets multiple form type options at once
ArrayFilter
Filters properties that store arrays or collections of values. Applied by default to Doctrine array fields. Renders a comparison selector (contains/not contains) and a multi-select input for the values to match:
1 2 3 4 5 6 7
use EasyCorp\Bundle\EasyAdminBundle\Filter\ArrayFilter;
$filters->add(ArrayFilter::new('tags')->setChoices([
'Tech' => 'tech',
'News' => 'news',
'Sports' => 'sports',
]));
Options:
setChoices(array $choices): defines the available choices to filter bysetTranslatableChoices(array $choices): same as above but with translatable labelscanSelectMultiple()(true): allows selecting multiple values
BooleanFilter
Filters boolean properties. Applied by default to boolean fields. Renders two radio buttons labeled "Yes" and "No":
1 2 3
use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
$filters->add(BooleanFilter::new('published'));
This filter has no additional options.
ChoiceFilter
Filters properties against a predefined list of choices. Renders a comparison selector and a dropdown (or checkboxes/radio buttons) with the available choices:
1 2 3 4 5 6 7
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
$filters->add(ChoiceFilter::new('status')->setChoices([
'Draft' => 'draft',
'Published' => 'published',
'Archived' => 'archived',
]));
Options:
setChoices(array $choices): defines the available choicessetTranslatableChoices(array $choices): defines choices with translatable labelscanSelectMultiple()(false): allows selecting multiple valuesrenderExpanded()(false): renders as checkboxes/radio buttons instead of dropdown
ComparisonFilter
A generic filter that combines a comparison operator selector with a value input.
Its form type is the base of the array, choice, text, date/time and numeric
filter form types. It's also applied by default to Doctrine dateinterval
properties. You can use it directly when you need a simple comparison filter
with custom form types:
1 2 3
use EasyCorp\Bundle\EasyAdminBundle\Filter\ComparisonFilter;
$filters->add(ComparisonFilter::new('score'));
This filter has no additional options beyond the common ones.
CountryFilter
Filters properties that store ISO 3166-1 country codes. Displays a dropdown with country names translated to the current locale using the Symfony Intl component:
1 2 3 4 5 6
use EasyCorp\Bundle\EasyAdminBundle\Filter\CountryFilter;
$filters->add(CountryFilter::new('country')
->includeOnly(['US', 'CA', 'MX'])
->preferredChoices(['US'])
);
Options:
includeOnly(array $countryCodes)(null): restricts choices to these country codes onlyremove(array $countryCodes)(null): removes these country codes from the choicespreferredChoices(array $countryCodes)(null): displays these countries at the top of the listuseAlpha3Codes()(false): uses ISO 3166-1 alpha-3 codes (e.g.USA) instead of alpha-2 codes (e.g.US)canSelectMultiple()(false): allows selecting multiple countriesrenderExpanded()(false): renders as checkboxes instead of dropdown
CurrencyFilter
Filters properties that store ISO 4217 currency codes. Displays a dropdown with currency names translated to the current locale using the Symfony Intl component:
1 2 3 4 5
use EasyCorp\Bundle\EasyAdminBundle\Filter\CurrencyFilter;
$filters->add(CurrencyFilter::new('currency')
->includeOnly(['USD', 'EUR', 'GBP', 'JPY'])
);
Options:
includeOnly(array $currencyCodes)(null): restricts choices to these currency codes onlyremove(array $currencyCodes)(null): removes these currency codes from the choicespreferredChoices(array $currencyCodes)(null): displays these currencies at the top of the listcanSelectMultiple()(false): allows selecting multiple currenciesrenderExpanded()(false): renders as checkboxes instead of dropdown
DateTimeFilter
Filters date and time properties. Applied by default to datetime, date,
and time fields. Renders a comparison selector (before/after/between/etc.)
and the browser's native date/time picker:
1 2 3
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter;
$filters->add(DateTimeFilter::new('createdAt'));
This filter has no additional options. The comparison operators include: equals, not equals, after, after or on, before, before or on, and between.
EntityFilter
Filters properties with Doctrine associations (ManyToOne, OneToMany, ManyToMany, OneToOne). Renders a comparison selector and a dropdown with related entities:
1 2 3 4 5 6
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
$filters->add(EntityFilter::new('category'));
// with autocomplete for large datasets
$filters->add(EntityFilter::new('author')->autocomplete());
Options:
autocomplete()(false): loads choices dynamically via AJAX requests (recommended for large datasets) using the same autocomplete mechanism as association fieldscanSelectMultiple()(false): allows selecting multiple entities
LanguageFilter
Filters properties that store ISO 639-1 language codes. Displays a dropdown with language names translated to the current locale using the Symfony Intl component:
1 2 3 4 5
use EasyCorp\Bundle\EasyAdminBundle\Filter\LanguageFilter;
$filters->add(LanguageFilter::new('language')
->includeOnly(['en', 'es', 'fr', 'de', 'it', 'pt'])
);
Options:
includeOnly(array $languageCodes)(null): restricts choices to these language codes onlyremove(array $languageCodes)(null): removes these language codes from the choicespreferredChoices(array $languageCodes)(null): displays these languages at the top of the listuseAlpha3Codes()(false): uses ISO 639-2 alpha-3 codes (e.g.eng) instead of alpha-2 (e.g.en)canSelectMultiple()(false): allows selecting multiple languagesrenderExpanded()(false): renders as checkboxes instead of dropdown
LocaleFilter
Filters properties that store locale codes. Displays a dropdown with locale names (language + region) translated to the current locale using the Symfony Intl component:
1 2 3 4 5
use EasyCorp\Bundle\EasyAdminBundle\Filter\LocaleFilter;
$filters->add(LocaleFilter::new('locale')
->includeOnly(['en_US', 'en_GB', 'es_ES', 'fr_FR', 'de_DE'])
);
Options:
includeOnly(array $localeCodes)(null): restricts choices to these locale codes onlyremove(array $localeCodes)(null): removes these locale codes from the choicespreferredChoices(array $localeCodes)(null): displays these locales at the top of the listcanSelectMultiple()(false): allows selecting multiple localesrenderExpanded()(false): renders as checkboxes instead of dropdown
NullFilter
Filters properties based on whether they are null or not null. Renders two radio buttons for the "is null" and "is not null" options. This filter is not applied automatically to any field:
1 2 3
use EasyCorp\Bundle\EasyAdminBundle\Filter\NullFilter;
$filters->add(NullFilter::new('deletedAt'));
Options:
setChoiceLabels(string $nullLabel, string $notNullLabel): customizes the labels for the radio buttons (default: "Is Null" and "Is Not Null")
NumericFilter
Filters numeric properties (integers, floats, decimals). Applied by default to numeric fields. Renders a comparison selector (equals/greater than/less than/ between/etc.) and one or two number inputs:
1 2 3
use EasyCorp\Bundle\EasyAdminBundle\Filter\NumericFilter;
$filters->add(NumericFilter::new('price'));
This filter has no additional options. The comparison operators include: equals, not equals, greater than, greater than or equal, less than, less than or equal, and between (which shows two inputs for min/max values).
TextFilter
Filters string and text properties. Applied by default to string and text fields. Renders a comparison selector (contains/starts with/ends with/etc.) and a text input:
1 2 3
use EasyCorp\Bundle\EasyAdminBundle\Filter\TextFilter;
$filters->add(TextFilter::new('title'));
This filter has no additional options. The comparison operators include: contains, not contains, equals, not equals, starts with, ends with, and is empty/is not empty.
TimezoneFilter
Filters properties that store timezone identifiers. Displays a dropdown with timezone names (with UTC offsets) translated to the current locale using the Symfony Intl component:
1 2 3 4
use EasyCorp\Bundle\EasyAdminBundle\Filter\TimezoneFilter;
// show only timezones for a specific country
$filters->add(TimezoneFilter::new('timezone')->forCountryCode('US'));
Options:
forCountryCode(string $countryCode)(null): restricts choices to timezones of the given countryincludeOnly(array $timezoneIds)(null): restricts choices to these timezone identifiers onlyremove(array $timezoneIds)(null): removes these timezone identifiers from the choicespreferredChoices(array $timezoneIds)(null): displays these timezones at the top of the listcanSelectMultiple()(false): allows selecting multiple timezonesrenderExpanded()(false): renders as checkboxes instead of dropdown
Custom Filters
If your needs are more specific, you can create your own filters, in a similar way to how you create custom fields. A filter is defined using two classes:
- A config class implementing
EasyCorpis used to configure the filter options and to apply the search conditions when the filter is active;\Bundle \EasyAdminBundle \Contracts \Filter \FilterInterface - A form class extending
Symfony\Component\Form\AbstractTypeis used to render the HTML widgets used to input the filter data in the application.
You can use the FilterTrait in your filter config class to avoid implementing
all the common methods. That way you only need to implement the apply()
method, which receives the filter form data and the QueryBuilder to customize
the query.
Consider this example which creates a custom date filter with some special values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// src/Controller/Admin/Filter/DateCalendarFilter.php
namespace App\Controller\Admin\Filter;
use App\Form\Type\Admin\DateCalendarFilterType;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto;
use EasyCorp\Bundle\EasyAdminBundle\Filter\FilterTrait;
use Symfony\Contracts\Translation\TranslatableInterface;
class DateCalendarFilter implements FilterInterface
{
use FilterTrait;
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): self
{
return (new self())
->setFilterFqcn(__CLASS__)
->setProperty($propertyName)
->setLabel($label)
->setFormType(DateCalendarFilterType::class);
}
public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
{
if ('today' === $filterDataDto->getValue()) {
$queryBuilder->andWhere(sprintf('%s.%s = :today', $filterDataDto->getEntityAlias(), $filterDataDto->getProperty()))
->setParameter('today', (new \DateTime('today'))->format('Y-m-d'));
}
// ...
}
}
Then, create the associated form type that renders for example a <select>
widget with some predefined values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// src/Form/Type/Admin/DateCalendarFilterType.php
namespace App\Form\Type\Admin;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DateCalendarFilterType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'choices' => [
'Today' => 'today',
'This month' => 'this_month',
// ...
],
]);
}
public function getParent()
{
return ChoiceType::class;
}
}
You can now use this custom filter in any of your dashboards and CRUD controllers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
namespace App\Controller\Admin;
use App\Controller\Admin\Filter\DateCalendarFilter;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class UserCrudController extends AbstractCrudController
{
// ...
public function configureFilters(Filters $filters): Filters
{
return $filters
// ...
->add(DateCalendarFilter::new('signupDate'))
;
}
}
Unmapped Filters
By default, each filter must be associated with a property of the entity.
However, sometimes you need a filter that doesn't match any property, similar to
the unmapped fields of forms (e.g. a "full name" filter
that searches both the firstName and lastName properties of a
Customer entity). In those cases, set the mapped option to false in
the filter; otherwise EasyAdmin throws an exception:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
namespace App\Controller\Admin;
use App\Controller\Admin\Filter\FullNameFilter;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class CustomerCrudController extends AbstractCrudController
{
// ...
public function configureFilters(Filters $filters): Filters
{
return $filters
// 'fullName' doesn't exist as a property of 'Customer' so it's
// defined as 'not mapped' to avoid errors
->add(FullNameFilter::new('fullName')->setFormTypeOption('mapped', false))
;
}
}
You don't need unmapped filters to filter by the properties of a related entity. Use the dot syntax explained in Filters on Associated Properties instead.