Choice2Type

FormType based on ChoiceType.

$choices = [ 'Eel' => 'eel', 'Salmon' => 'salmon', 'Cat' => 'cat', ]; $getSpecie = function ($Mission) { switch ($Mission) { case 'eel': case 'salmon': return 'Fish'; default: return 'Other'; } }; // --- select $builder->add('choiceMission', Choice2Type::class, [ 'label' => 'Select', 'choices' => $choices, 'required' => false, ]); $builder->add('choiceMissionReadonly', Choice2Type::class, [ 'label' => 'Select readonly', 'choices' => $choices, 'required' => false, 'attr' => [ 'readonly' => true ] ]); // --- select multiple $builder->add('choiceMissions', Choice2Type::class, [ 'label' => 'Select multiple', 'choices' => $choices, 'required' => false, 'multiple' => true ]); // --- select with html template $builder->add('choiceMissionTemplated', Choice2Type::class, [ 'label' => 'Select with html template', 'choices' => $choices, 'expose' => function ($choice) use ($getSpecie) { return ['specie' => $getSpecie($choice)]; }, 'required' => false, 'template' => '[[text]]
[[specie]]', ]); // --- select with grouped options $builder->add('choiceMissionGrouped', Choice2Type::class, [ 'label' => 'Select with grouped options', 'choices' => $choices, 'group_by' => $getSpecie, 'required' => false, ]);

Options

All options of ChoiceType +

Option Type Default value Description
min_search_length int 0 Start to filter options after n characters typed.
expose callable null Used with option template_selector or template_html to expose additional variables.
template_html string null Mustache template (by default, 2 variables are available : id and text).
template_selector string null Css selector of mustache template in HTML dom.

Entity2Type

FormType based on EntityType.

// --- select on doctrine entity $builder->add('choiceMissionEntity', Entity2Type::class, [ 'label' => 'Select on doctrine entity', 'class' => SpaceMission::class, 'required' => false, ]);

Options

All options of EntityType + all specific options of Choice2Type (min_search_length, ...)

AutoCompleteType

FormType with async loading (Ajax request).

Select
Select with html template
Select multiple
Select with pagination
// Form // --- select with async loading $builder->add('asyncChoiceMission', AutocompleteType::class, [ 'label' => 'Select', 'route' => 'app_admin_form_loadmission', 'class' => SpaceMission::class, 'required' => false, ]); // Controller /** * @Route("/load") */ public function load(Request $request) { // without pagination $q = $request->query->get('q'); return new JsonResponse([ [ 'id' => 0, // doctrine id 'text' => 'Eel', // Label // 'foo' => 'bar' // Expose additional data for mustache template ], [ 'id' => 1, 'text' => 'Salmon' ], ]); // with pagination $q = $request->query->get('q'); $page = $request->query->getInt('page', 1); return new JsonResponse([ 'results' => [ // result of page $page [ 'id' => 0, 'text' => 'Eel', ], [ 'id' => 1, 'text' => 'Salmon' ] ], 'more' => true // true if there is results on page $page + 1 ]); }

Options

Option Type Default value Description
route string null Mandatory, route name of action returning results
route_params array [] Route parameters
class string null Mandatory, doctrine entity classname.
multiple bool false Multiple selection if true.
min_search_length int 0 Start to filter options after n characters typed.
template_html string null Mustache template (by default, 2 variables are available : id and text).
template_selector string null Css selector of mustache template in HTML dom.