CakePHP 表单助手 - 选择 - 如何为选择元素添加占位符


CakePHP Form Helper - Select - How do I add a Placeholder for a select element?

如何使用

CakePHP 2 在选择(下拉(中获得类似默认选项的占位符?

现在我有以下内容

<?php echo $this->Form->input('gender', array('options' => array('male' => 'Male', 'female' => 'Female'), 'empty' => '','label' => '','class'=>'scale')); ?>

我想有一个灰色的默认值"性别",以便用户真正知道下拉菜单的作用。我也不希望表单发送该值。

如果是原始的 HTML,那么我想它只是

selected="selected" disabled="disabled"

应该是。

$this->Form->input(
    'gender', 
    array(
        'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),           
        'empty' => 'Your placeholder will goes here',
        'label' => '',
        'class'=>'scale'
    )
);

我宁愿做这样的事情

$this->Form->input(
    'gender', 
    array(
        'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),           
        'empty' => '',
        'label' => '',
        'class'=>'scale'
    )
);

你找empty,可以把它作为第三个参数传递给$this->Form->select()

array('empty' => array(0 => '-- Select --'))


文档

在此处查看文档: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>

如果选项中不需要值,则可以删除键并仅传递字符串值。

放置占位符的最简单方法:

echo $this->Form->input('User.role_id', array(
     'options' => $roles,
     'empty' => 'Choose', 
));