Select
This is classic select dropdown that is added like this:
<?php
$this->add('languages', 'select', [
'choices' => ['en' => 'English', 'fr' => 'French'],
'selected' => 'en',
'empty_value' => '=== Select language ==='
]);
Beside inherited it have 3 additional options:
choices
(Array) (Default:[]
) - key value pairs used for options in the selectempty_value
(String) (Default:null
) - If provided, added to the start of select as empty value-
selected
(StringArray Closure) (Default: null
) - Option that needs to be selected. If not provided, Form class will try to fetch it from passed model.- array is used when ‘multiple’ attribute is set
- Closure is used for modifying model data before passed to view. Useful when fetching relationship data to pluck only data that is needed.
<?php
$this->add('languages', 'select', [
'choices' => ['en' => 'English', 'fr' => 'French'],
'selected' => function ($data) {
// Returns the array of short names from model relationship data
return array_pluck($data, 'short_lang_name');
}
'empty_value' => '=== Select language ==='
])