PHP code example of b2pweb / bdf-form-filter-prime

1. Go to this page and download the library: Download b2pweb/bdf-form-filter-prime 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/ */

    

b2pweb / bdf-form-filter-prime example snippets




namespace App\Form;

use Bdf\Form\Filter\FilterForm;
use Bdf\Form\Filter\FilterFormBuilder;

class MyFilters extends FilterForm
{
    public function configureFilters(FilterFormBuilder $builder): void
    {
        // Build filter fields
        // Will add a "foo LIKE xxx%"
        $builder->searchBegins('foo');

        // Will add a "age BETWEEN ? AND ?"
        $builder->embedded('age', function ($builder) {
            $builder->integer('0')->setter();
            $builder->integer('1')->setter();
        })->between();
        
        // Define a custom attribute name and operator
        $builder->string('foo')->criterion('bar')->operator('>=');
    }
}



// Instantiate the form (a container can be use for handle dependency injection)
$form = new MyFilters();

// Submit form
// Note: if some constraints has been added, call `$form->valid()` and `$form->error()` to check errors
$form->submit($request->query->all());

// Get generated criteria
$criteria = $form->value();

// Call prime with criteria
$list = MyEntity::where($criteria->all())->paginate();



namespace App\Form;

use Bdf\Form\Filter\FilterForm;
use Bdf\Form\Filter\FilterFormBuilder;

class MyFilters extends FilterForm
{
    public function configureFilters(FilterFormBuilder $builder): void
    {
        // Set the entity class (note: use $this instead of $builder)
        $this->setEntity(Person::class);

        // Define filters
        $builder->searchBegins('firstName');
        $builder->searchBegins('lastName');
        $builder->embedded('age', function ($builder) {
            $builder->integer('0')->setter();
            $builder->integer('1')->setter();
        })->between();
    }
}



// Get the form instance, using a container to inject prime
$form = $container->get(MyFilters::class);

// Submit form
$form->submit($request->query->all());

// Use apply to modify the query
$query = Person::builder();
$entities = $form->apply($query)->all(); // Apply filters and execute query

// Use directly query() method to create the filter query
$entities = $form->query();



namespace App\Form;

use Bdf\Form\Filter\MongoFilterForm;
use Bdf\Form\Filter\FilterFormBuilder;

class MyFilters extends MongoFilterForm
{
    public function configureFilters(FilterFormBuilder $builder): void
    {
        // Set the document class (note: use $this instead of $builder)
        $this->setEntity(Person::class);

        // Define filters
        $builder->searchBegins('firstName');
        $builder->searchBegins('lastName');
        $builder->embedded('age', function ($builder) {
            $builder->integer('0')->setter();
            $builder->integer('1')->setter();
        })->between();
    }
}