如何使用CakePHP 2在select(下拉列表)中获取类似默认选项的占位符?
现在我有以下
<?php echo $this->Form->input('gender', array('options' => array('male' => 'Male', 'female' => 'Female'), 'empty' => '','label' => '','class'=>'scale')); ?>
我希望默认值为“性别”,因此用户实际上知道下拉菜单的作用。我也不希望表单发送该值。
如果是原始HTML,那么我猜它只是
selected="selected" disabled="disabled"
答案 0 :(得分:4)
应该是。
$this->Form->input(
'gender',
array(
'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),
'empty' => 'Your placeholder will goes here',
'label' => '',
'class'=>'scale'
)
);
答案 1 :(得分:2)
我宁愿做这样的事情
$this->Form->input(
'gender',
array(
'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),
'empty' => '',
'label' => '',
'class'=>'scale'
)
);
答案 2 :(得分:0)
您正在寻找empty
,您可以将此作为第三个参数传递给$this->Form->select()
。
array('empty' => array(0 => '-- Select --'))
View the docs here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
/**
* Method Signature
*
* public function select($fieldName, $options = array(), $attributes = array())
*
* ### Attributes:
*
* - `showParents` - If included in the array and set to true, an additional option element
* will be added for the parent of each option group. You can set an option with the same name
* and it's key will be used for the value of the option.
* - `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.
* - `value` The selected value of the input.
* - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
* - `disabled` - Control the disabled attribute. When creating a select box, set to true to disable the
* select box. When creating checkboxes, `true` will disable all checkboxes. You can also set disabled
* to a list of values you want to disable when creating checkboxes.
*
*/
<强> PHP:强>
// Example..
$this->Form->select(
'model', // First param = fieldName
$options, // Second param = options
array('empty' => array(0 => '-- Select --')) // Third param = attributes
);
HTML :(已呈现)
// Renders
<select>
<option value="0">-- Select --</option>
</select>
如果您不需要选项中的值,则可以删除密钥并传递字符串值。
答案 3 :(得分:0)
放置占位符的最简单方法:
echo $this->Form->input('User.role_id', array(
'options' => $roles,
'empty' => 'Choose',
));