PHP code example of openforce / select2-bundle

1. Go to this page and download the library: Download openforce/select2-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/ */

    

openforce / select2-bundle example snippets


// config/bundles.php

return [
    // ...
    \Openforce\Select2Bundle\OpenforceSelect2Bundle::class => ['all' => true],
];

// your Controller file
    public function index(){

        $formBuildr = $this->createFormBuilder();
        $formBuilder->add('price', InputType::class);
        $formBuilder->add("product", Select2Type::class,[
            'class' => Product::class, 
            'search_field' => 'name', 
            'choice_label' => 'name',
            'max_results' => 50, 
            'related_fields' => ['price'],
            'filter' => function(EntityRepository $er, $value, $relatedFields){
                return $er->createQueryBuilder("p")
                    ->where("p.description like :word and p.price < :price")
                    ->setParameter("word", "%".$value."%")
                    ->setParameter('price', $relatedFields['price'])
                    ->orderBy("p.productId")
                    ;
            }
        ])
    }