添加禁用(和选定)选项以选择带有表单助手的项目

Rie*_* C. 4 cakephp formhelper

嗨,我正在尝试使用表单助手向选择框添加禁用选项我使用此代码生成一个额外的空字段,但我希望禁用此字段.

echo $this->Form->input('User.usertype_id',array('type'=>'select', 'empty'=>'usertype');
Run Code Online (Sandbox Code Playgroud)

这会产生:

<div class="input select">
    <label for="UserUsertypeId">Usertype</label>
    <select name="data[User][usertype_id]" id="UserUsertypeId">
        <option value="">usertype</option>
        <option value="1">athlete</option>
        <option value="2">trainer</option>
    </select>
</div>
Run Code Online (Sandbox Code Playgroud)

但我想要这个:

<div class="input select">
    <label for="UserUsertypeId">Usertype</label>
    <select name="data[User][usertype_id]" id="UserUsertypeId">
        <option value="" disabled="disabled" selected="selected">usertype</option>
        <option value="1">athlete</option>
        <option value="2">trainer</option>
    </select>
</div>
Run Code Online (Sandbox Code Playgroud)

有没有办法简单地做到这一点,或者我应该只使用一些js?

ste*_*ove 10

如果您事先知道这些选项,则可以构建$options要在选择菜单中使用的数组.这应该给你你想要的东西:

$options = array(
            array(
                'name' => 'usertype',
                'value' => '',
                'disabled' => TRUE,
                'selected' => TRUE
            ),
            'athlete',
            'trainer'
            );

echo $this->Form->input('User.usertype_id', array('type' => 'select', 'options' => $options));
Run Code Online (Sandbox Code Playgroud)

或者这可能有用,但我还没有测试过:

echo $ this-> Form-> input('User.usertype_id',array('type'=>'select','empty'=> array('text'=>'usertype','selected'=> TRUE, 'disabled'=> FALSE)));


Mic*_*sky 5

我知道这个问题上次更新是在 2010 年,但我有实际的答案。查看 CakePHP 文档中的示例:

$options = array(
    'Value 1' => 'Label 1',
    'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
    'multiple' => 'checkbox',
    'disabled' => array('Value 1')
));
Run Code Online (Sandbox Code Playgroud)

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select