PHP code example of padam87 / form-filter-bundle

1. Go to this page and download the library: Download padam87/form-filter-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

padam87 / form-filter-bundle example snippets


use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('email', TextType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver
            ->setDefaults(
                [
                    'method' => Request::METHOD_GET,
                    'csrf_protection' => false,
                ]
            )
        ;
    }
}

$filters = $this->createForm(UserFilterType::class);
$filters->handleRequest($request);

$qb = $em->getRepository(User::class)->createQueryBuilder('alias');

$this->get('padam87_form_filter.filters')->apply($qb, $filters);

// paginate, render template etc.

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, ['filter_expr' => 'like'])
            ->add('email', TextType::class, ['filter_expr' => 'like'])
        ;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, ['filter_expr' => 'like'])
            ->add('email', TextType::class, ['filter_expr' => 'like'])
            ->add(
                'city',
                TextType::class,
                [
                    'filter' => function(QueryBuilder $qb, $alias, $value) {
                        $qb
                            //->join('u.address', 'a')
                            ->andWhere($qb->expr->eq('a.city', ':city'))
                            ->setParameter('city', $value)
                        ;
                    
                        return $qb;
                    }
                ]
            )
        ;
    }

$builder
    ->add(
        'createdAt',
        RangeFilterType::class,
        [
            'from_field_type' => DateType::class,
            'from_field_options' => [
                'widget' => 'single_text',
            ],
            'to_field_type' => DateType::class,
            'to_field_options' => [
                'widget' => 'single_text',
            ],
            'to_field_expr' => 'lt'
        ]
    )
;