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);
}
/** * 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); }