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

chevron-rightYamlhashtag
config/packages/sylius_grid.yaml
sylius_grid:
    grids:
        app_user:
            filters:
                username:
                    type: string
                email:
                    type: string
                firstName:
                    type: string
                lastName:
                    type: string
chevron-rightPHPhashtag
config/packages/sylius_grid.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

src/Grid/UserGrid.php
<?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

chevron-rightYamlhashtag
config/packages/sylius_grid.yaml
sylius_grid:
    grids:
        app_user:
            filters:
                search:
                    type: string
                    options:
                        fields: [username, email, firstName, lastName]
chevron-rightPHPhashtag
config/packages/sylius_grid.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

src/Grid/UserGrid.php
<?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:

chevron-rightYamlhashtag
chevron-rightPHPhashtag

OR

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

chevron-rightYamlhashtag

This filter checks if a value is true or false.

chevron-rightPHPhashtag

OR

Date

This filter checks if a chosen datetime field is between given dates.

chevron-rightYamlhashtag
chevron-rightPHPhashtag

OR

Entity

This type filters by a chosen entity.

chevron-rightYamlhashtag
chevron-rightPHPhashtag

OR

Money

This filter checks if an amount is within the specified range and is in the selected currency

chevron-rightYamlhashtag
chevron-rightPHPhashtag

OR

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

chevron-rightYamlhashtag
chevron-rightPHPhashtag

OR

Select

This type filters by a value chosen from the defined list

chevron-rightYamlhashtag
chevron-rightPHPhashtag

OR

Custom Filters

Tip

If you need to create a custom filter, read the docs here.

Last updated