select() public method

### Attributes: - multiple - show a multiple select box. If set to 'checkbox' multiple checkboxes will be created instead. - empty - If true, the empty select option is shown. If a string, that string is displayed as the empty element. - escape - If true contents of options will be HTML entity encoded. Defaults to true. - val The selected value of the input. - disabled - Control the disabled attribute. When creating a select box, set to true to disable the select box. Set to an array to disable specific option elements. ### Using options A simple array will create normal options: $options = [1 => 'one', 2 => 'two']; $this->Form->select('Model.field', $options)); While a nested options array will create optgroups with options inside them. $options = [ 1 => 'bill', 'fred' => [ 2 => 'fred', 3 => 'fred jr.' ] ]; $this->Form->select('Model.field', $options); If you have multiple options that need to have the same value attribute, you can use an array of arrays to express this: $options = [ ['text' => 'United states', 'value' => 'USA'], ['text' => 'USA', 'value' => 'USA'], ];
See also: Cake\View\Helper\FormHelper::multiCheckbox() for creating multiple checkboxes.
public select ( string $fieldName, array | Traversable $options = [], array $attributes = [] ) : string
$fieldName string Name attribute of the SELECT
$options array | Traversable Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the SELECT element
$attributes array The HTML attributes of the select element.
return string Formatted SELECT element
Beispiel #1
0
 /**
  * Returns a formatted SELECT element
  * @param string $fieldName Name attribute of the SELECT
  * @param array|\Traversable $options Array of the OPTION elements
  *  (as 'value'=>'Text' pairs) to be used in the SELECT element
  * @param array $attributes The HTML attributes of the select element
  * @return string
  */
 public function select($fieldName, $options = [], array $attributes = [])
 {
     if (empty($attributes['default']) && empty($attributes['value'])) {
         $attributes = $this->optionsDefaults(['empty' => true], $attributes);
     }
     return parent::select($fieldName, $options, $attributes);
 }
 public function select($fieldName, $options = [], array $attributes = [])
 {
     $attributes = $this->_injectStyles($attributes, 'form-control');
     return parent::select($fieldName, $options, $attributes);
 }