Filters
Filters on grids act as predefined search options for each grid. Having a grid of objects you can filter out only those with a specified name, or value etc. Here you can find the supported filters. Keep in mind you can very easily define your own ones!
String
Simplest filter type. It can filter by one or multiple fields.
Filter by one field
<?php
declare(strict_types=1);
namespace App\Grid;
use App\Entity\User;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Component\Grid\Attribute\AsGrid;
#[AsGrid(
resourceClass: User::class,
name: 'app_user',
)]
final class UserGrid extends AbstractGrid
{
public function __invoke(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->withFilters(
Filter::create('username', 'string'),
Filter::create('email', 'string'),
Filter::create('firstName', 'string'),
Filter::create('lastName', 'string'),
)
// can be simplified using StringFilter
->withFilters(
StringFilter::create('username'),
StringFilter::create('email'),
StringFilter::create('firstName'),
StringFilter::create('lastName'),
)
;
}
}Filter by multiple fields
Search options
This filter allows the user to select the following search options:
contains
not contains
equal
not equal
starts with
ends with
empty
not empty
in
not in
member of
If you don't want to display all these matching possibilities, you can choose just one of them. Then only the input field will be displayed. You can achieve it like this:
By configuring the filter as shown above, you will create an input field that filters user objects based on whether their username contains a given string.
Boolean
This filter checks if a value is true or false.
Date
This filter checks if a chosen datetime field is between given dates.
Entity
This type filters by a chosen entity.
Money
This filter checks if an amount is within the specified range and is in the selected currency
Warning
Providing different scale values between form_options and options may cause unwanted, and plausibly volatile results.
Exists
This filter checks if the specified field contains any value
Select
This type filters by a value chosen from the defined list
Creating a custom Filter
Sylius Grids come with built-in filters, but there are use-cases where you need something more than a basic filter. Grids let you define your own filter types!
To add a new filter, we need to create an appropriate class and form type.
And the form type:
And the form type:
Create a template for the filter, similar to the existing ones:
If you use autoconfiguration, the filter is automatically registered as a grid filter.
But if you don't use autoconfiguration, let's register your new filter type as a service.
Now you can use your new filter type in any grid configuration!
OR
Last updated
Was this helpful?