Cake\View\Helper\FormHelper::select PHP Method

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
    public function select($fieldName, $options = [], array $attributes = [])
    {
        $attributes += ['disabled' => null, 'escape' => true, 'hiddenField' => true, 'multiple' => null, 'secure' => true, 'empty' => false];
        if ($attributes['multiple'] === 'checkbox') {
            unset($attributes['multiple'], $attributes['empty']);
            return $this->multiCheckbox($fieldName, $options, $attributes);
        }
        // Secure the field if there are options, or it's a multi select.
        // Single selects with no options don't submit, but multiselects do.
        if ($attributes['secure'] && empty($options) && empty($attributes['empty']) && empty($attributes['multiple'])) {
            $attributes['secure'] = false;
        }
        $attributes = $this->_initInputField($fieldName, $attributes);
        $attributes['options'] = $options;
        $hidden = '';
        if ($attributes['multiple'] && $attributes['hiddenField']) {
            $hiddenAttributes = ['name' => $attributes['name'], 'value' => '', 'form' => isset($attributes['form']) ? $attributes['form'] : null, 'secure' => false];
            $hidden = $this->hidden($fieldName, $hiddenAttributes);
        }
        unset($attributes['hiddenField'], $attributes['type']);
        return $hidden . $this->widget('select', $attributes);
    }

Usage Example

Example #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);
 }
All Usage Examples Of Cake\View\Helper\FormHelper::select