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
Yaml
sylius_grid:
grids:
app_user:
filters:
username:
type: string
email:
type: string
firstName:
type: string
lastName:
type: string
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(Filter::create('username', 'string'))
->addFilter(Filter::create('email', 'string'))
->addFilter(Filter::create('firstName', 'string'))
->addFilter(Filter::create('lastName', 'string'))
// can be simplified using StringFilter
->addFilter(StringFilter::create('username'))
->addFilter(StringFilter::create('email'))
->addFilter(StringFilter::create('firstName'))
->addFilter(StringFilter::create('lastName'))
)
};
OR
<?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\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(Filter::create('username', 'string'))
->addFilter(Filter::create('email', 'string'))
->addFilter(Filter::create('firstName', 'string'))
->addFilter(Filter::create('lastName', 'string'))
// can be simplified using StringFilter
->addFilter(StringFilter::create('username'))
->addFilter(StringFilter::create('email'))
->addFilter(StringFilter::create('firstName'))
->addFilter(StringFilter::create('lastName'))
;
}
public function getResourceClass(): string
{
return User::class;
}
}
Filter by multiple fields
Yaml
sylius_grid:
grids:
app_user:
filters:
search:
type: string
options:
fields: [username, email, firstName, lastName]
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('username', 'string')
->setOptions(['fields' => ['username', 'email', 'firstName', 'lastName']])
)
// can be simplified using StringFilter
->addFilter(
StringFilter::create('username', ['username', 'email', 'firstName', 'lastName'])
)
)
};
OR
<?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\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('username', 'string')
->setOptions(['fields' => ['username', 'email', 'firstName', 'lastName']])
)
// can be simplified using StringFilter
->addFilter(
StringFilter::create('username', ['username', 'email', 'firstName', 'lastName'])
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
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:
Yaml
sylius_grid:
grids:
app_user:
filters:
username:
type: string
form_options:
type: contains
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('username', 'string')
->setFormOptions([
'type' => 'contains',
])
)
// can be simplified using StringFilter
->addFilter(
StringFilter::create('username', null, 'contains')
)
)
};
OR
<?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\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('username', 'string')
->setFormOptions([
'type' => 'contains',
])
)
// can be simplified using StringFilter
->addFilter(
StringFilter::create('username', null, 'contains')
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
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
Yaml
This filter checks if a value is true or false.
sylius_grid:
grids:
app_channel:
filters:
enabled:
type: boolean
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\BooleanFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('enabled', 'boolean')
)
// can be simplified using BooleanFilter
->addFilter(
BooleanFilter::create('enabled')
)
)
};
OR
<?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\BooleanFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('enabled', 'boolean')
)
// can be simplified using BooleanFilter
->addFilter(
BooleanFilter::create('enabled')
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
Date
This filter checks if a chosen datetime field is between given dates.
Yaml
sylius_grid:
grids:
app_order:
filters:
createdAt:
type: date
completedAt:
type: date
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\DateFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(Filter::create('createdAt', 'date'))
->addFilter(Filter::create('completedAt', 'date'))
// can be simplified using DateFilter
->addFilter(DateFilter::create('createdAt'))
->addFilter(DateFilter::create('completedAt'))
)
};
OR
<?php
declare(strict_types=1);
namespace App\Grid;
use App\Entity\User;
use Sylius\Bundle\GridBundle\Builder\Filter\DateFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(Filter::create('createdAt', 'date'))
->addFilter(Filter::create('completedAt', 'date'))
// can be simplified using DateFilter
->addFilter(DateFilter::create('createdAt'))
->addFilter(DateFilter::create('completedAt'))
;
}
public function getResourceClass(): string
{
return User::class;
}
}
Entity
This type filters by a chosen entity.
Yaml
sylius_grid:
grids:
app_order:
filters:
channel:
type: entity
form_options:
class: "%app.model.channel.class%"
# You can pass any form options available in Entity Type
# See https://symfony.com/doc/current/reference/forms/types/entity.html
multiple: true
customer:
type: entity
form_options:
class: "%app.model.customer.class%"
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\EntityFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('channel', 'entity')
->setFormOptions([
'class' => '%app.model.channel.class%'
// You can pass any form options available in Entity Type
// See https://symfony.com/doc/current/reference/forms/types/entity.html
'multiple' => true,
])
)
->addFilter(
Filter::create('customer', 'entity')
->setFormOptions(['class' => '%app.model.customer.class%'])
)
// can be simplified using EntityFilter
->addFilter(
EntityFilter::create('channel', '%app.model.channel.class%')
->addFormOption('multiple', true)
)
->addFilter(
EntityFilter::create('customer', '%app.model.customer.class%')
)
)
};
OR
<?php
declare(strict_types=1);
namespace App\Grid;
use App\Entity\User;
use Sylius\Bundle\GridBundle\Builder\Filter\EntityFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('channel', 'entity')
->setFormOptions(['class' => '%app.model.channel.class%'])
)
->addFilter(
Filter::create('customer', 'entity')
->setFormOptions(['class' => '%app.model.customer.class%'])
)
// can be simplified using EntityFilter
->addFilter(
EntityFilter::create('channel', '%app.model.channel.class%')
)
->addFilter(
EntityFilter::create('customer', '%app.model.customer.class%')
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
Money
This filter checks if an amount is within the specified range and is in the selected currency
Yaml
sylius_grid:
grids:
app_order:
filters:
total:
type: money
form_options:
scale: 3
options:
currency_field: currencyCode
scale: 3
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\MoneyFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('total', 'money')
->setFormOptions(['scale' => 3])
->setOptions([
'currency_field' => 'currencyCode',
'scale' => 3,
])
)
// can be simplified using MoneyFilter
->addFilter(
MoneyFilter::create('total', 'currencyCode', 3)
)
)
};
OR
<?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\MoneyFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('total', 'money')
->setFormOptions(['scale' => 3])
->setOptions([
'currency_field' => 'currencyCode',
'scale' => 3,
])
)
// can be simplified using MoneyFilter
->addFilter(
MoneyFilter::create('total', 'currencyCode', 3)
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
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
Yaml
sylius_grid:
grids:
app_order:
filters:
date:
type: exists
options:
field: completedAt
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\ExistsFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('date', 'exists')
->setOptions(['field' => 'completedAt'])
)
// can be simplified using ExistsFilter
->addFilter(
ExistsFilter::create('date', 'completedAt')
)
)
};
OR
<?php
declare(strict_types=1);
namespace App\Grid;
use App\Entity\User;
use Sylius\Bundle\GridBundle\Builder\Filter\ExistsFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('date', 'exists')
->setOptions(['field' => 'completedAt'])
)
// can be simplified using ExistsFilter
->addFilter(
ExistsFilter::create('date', 'completedAt')
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
Select
This type filters by a value chosen from the defined list
Yaml
sylius_grid:
grids:
app_order:
filters:
state:
type: select
form_options:
choices:
sylius.ui.ready: Ready
sylius.ui.shipped: Shipped
PHP
<?php
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\SelectFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Config\GridConfig;
return static function (GridConfig $grid): void {
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
->addFilter(
Filter::create('state', 'select')
->setFormOptions([
'choices' => [
'sylius.ui.ready' => 'Ready',
'sylius.ui.shipped' => 'Shipped',
],
])
)
// can be simplified using SelectFilter
->addFilter(
SelectFilter::create('state', [
'sylius.ui.ready' => 'Ready',
'sylius.ui.shipped' => 'Shipped',
])
)
)
};
OR
<?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\SelectFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_user';
}
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addFilter(
Filter::create('state', 'select')
->setFormOptions([
'choices' => [
'sylius.ui.ready' => 'Ready',
'sylius.ui.shipped' => 'Shipped',
],
])
)
// can be simplified using SelectFilter
->addFilter(
SelectFilter::create('state', [
'sylius.ui.ready' => 'Ready',
'sylius.ui.shipped' => 'Shipped',
])
)
;
}
public function getResourceClass(): string
{
return User::class;
}
}
Custom Filters
Tip
If you need to create a custom filter, read the docs here.
Last updated