openforce/select2-bundle

symfony select2 entity form type

Installs: 435

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.1.1 2021-08-15 09:32 UTC

This package is auto-updated.

Last update: 2024-04-22 13:11:44 UTC


README

Introduction

This is a symfony Entity Form Type that supports select2 without using a controller. It's so easy and simple.

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require openforce/select2-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

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

Step 3: Configure twig.yaml file

twig:

    form_themes:
        - "@OpenforceSelect2/select2_type.html.twig"

Step 4: Add select2 to your template

//Add your template file
<script src="//code.jquery.com/jquery-1.9.0rc1.js"></script>
<link href="//cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="//cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

How to use

The main options are:

  • class Required. Your target entity.
  • search_field Required. Entity field name you want to use for your search.
  • choice_label Optional. Entity field name you want to display on the label. default value is __toString.
  • max_results Optional. Number to display on page.
  • filter Optional. It is callback function. If you use this, you don't need to define search_field.
  • related_fields Optional. This option allows you to use the values you enter in other fields with the filter option.
// 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")
                    ;
            }
        ])
    }