Practically in all projects there is a need to display a drop-down list in the form, given in which would be loaded using ajax. In this regard, 2 years ago I wrote a SuggestBundle for a symphony , which contains an additional type of form, which can be used instead of the standard entity
and document
types. The key feature of the bundle is that the drop-down list is loaded using ajax (using the select2 library). The other day, I finally braced myself to write documentation on the bundle and decided to share the development with the community.
In addition, the bundle can also be used to build drop-down lists that are not related to Doctrine ORM and Doctrine ODM .
Under the cut instructions for installing, configuring and using a bundle.
Link to the bundle: https://github.com/sirian/suggest-bundle .
Add the sirian/suggest-bundle
package to the require
section of the composer.json
file.
$ composer require sirian/suggest-bundle
Add SuggestBundle to your AppKernel.php:
<?php public function registerBundles() { $bundles = array( // ... new Sirian\SuggestBundle\SirianSuggestBundle(), // ... ); ... }
After installing the bundle, add the following lines to the routing configuration:
# app/config/routing.yml _sirian_suggest: resource: "@SirianSuggestBundle/Resources/config/routing.yml" prefix: /suggest
And select the version of the widget that will be used by default for forms (depending on the version of the select2 library that you will use in the project). Valid values are select2_v3
, select2_v4
. You can also specify other form options that will be used by default when using the form type SuggestType::class
# app/config/config.yml ... sirian_suggest: form_options: widget: select2_v4 attr: placeholder: "Search..."
For documents from Doctrine ODM and Doctrine ORM entities, you can easily describe the necessary sagsters in your project's config.yml file.
# app/config/config.yml ... sirian_suggest: odm: category: class: "MainBundle:Category" property: name user: class: "MainBundle:User" property: username search: email: ~ username: ~
# app/config/config.yml ... sirian_suggest: orm: category: class: "MainBundle:Category" property: name user: class: "MainBundle:User" property: username search: email: ~ username: ~
If you need additional logic to build a drop-down list - you can create your own saggester. For example, let's create an AdminSuggester
that will contain only those users who have the role ROLE_ADMIN
. For simplicity, we can inherit a class from the DocumentSuggester abstract class (or EntitySuggester for Doctrine ORM).
<?php namespace App\MainBundle\Suggest; use App\MainBundle\Document\User; use Doctrine\Common\Persistence\ManagerRegistry; use Sirian\SuggestBundle\Suggest\DocumentSuggester; use Sirian\SuggestBundle\Suggest\Item; use Sirian\SuggestBundle\Suggest\SuggestQuery; class AdminSuggester extends DocumentSuggester { public function __construct(ManagerRegistry $registry) { $options = [ 'class' => User::class, 'id_property' => 'id', 'property' => 'username', // , 'search' => ['name' => 1, 'username' => 1, 'email' => 1] ]; parent::__construct($registry, $options); } protected function createSuggestQueryBuilder(SuggestQuery $query) { $qb = parent::createSuggestQueryBuilder($query); $qb->field('roles')->equals('ROLE_ADMIN'); return $qb; } }
Describe the new service in services.yml
with the sirian_suggest.suggester
tag
app.suggester.admin: class: App\MainBundle\Suggest\AdminSuggester arguments: ["@doctrine_mongodb"] tags: - {name: 'sirian_suggest.suggester', alias: 'admin'}
The admin
alias will be used in the suggester
parameter for the form type SuggestType::class
and in the url /suggest/admin
.
$formBuilder->add('category', SuggestType::class, [ 'suggester' => 'category' ])
PS More information on using the bundle and additional functionality can be found at https://github.com/sirian/suggest-bundle .
Source: https://habr.com/ru/post/304572/
All Articles