Cakephp 添加禁用(和选中)选项以使用表单帮助器选择项目
嗨 我正在尝试使用表单帮助器将禁用的选项添加到选择框中。我使用此代码生成一个额外的空字段,但我希望禁用此字段Cakephp 添加禁用(和选中)选项以使用表单帮助器选择项目,cakephp,formhelper,Cakephp,Formhelper,嗨 我正在尝试使用表单帮助器将禁用的选项添加到选择框中。我使用此代码生成一个额外的空字段,但我希望禁用此字段 echo $this->Form->input('User.usertype_id',array('type'=>'select', 'empty'=>'usertype'); 这将产生: <div class="input select"> <label for="UserUsertypeId">Usertype</lab
echo $this->Form->input('User.usertype_id',array('type'=>'select', 'empty'=>'usertype');
这将产生:
<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>
用户类型
用户类型
运动员
教练
但我想要这个:
<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>
用户类型
用户类型
运动员
教练
有什么方法可以简单地做到这一点,或者我应该使用一些js吗?如果您事先知道这些选项,您可以构建
$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));
或者这可能有效,但我也没有测试过:
echo$this->Form->input('User.usertype_id',array('type'=>'select','empty'=>array('text'=>'usertype','selected'=>TRUE',disabled'=>FALSE)) mhmm看起来不可能在注释中添加一些代码块 因此,您的两个选项都会生成:
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="text">usertype</option>
<option value="selected">1</option>
<option value="disabled"></option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
这将生成一个值为(“disabled=“disabled”selected=“selected”的选项)
因此,它变成:
...
<option value="" disabled="disabled" selected="selected"></option>
...
。。。
...
这是一个暂时的解决方案,直到我找到更好的,欢迎建议 我知道这个问题最近一次更新是在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')
));
我将韦普图努斯和史蒂夫洛夫的解决方案合并在一起 在控制器中:
$examples = $this->Test->Examples->find('list');
$this->set('examples', $examples);
在视图中
echo $this->Form->input('example_id', array('empty' => array(
'' => array(
'name' => __('Select an Example'),
'value' => '',
'disabled' => TRUE,
'selected' => TRUE
)
))
);
试试这个
$operations = [
'' => 'Select Operation',
'query' => 'Query',
'create' => 'Create',
'update' => 'Update',
'upsert' => 'Upsert',
'delete' => 'Delete'
];
echo $this->Form->input('operation[]',[
'type' => 'select',
'options' => $operations,
'class' => 'operation-class',
'id' => 'operation-id-1',
'required' => 'required',
'label' => false,
'disabled' => [''],
'value' => ''
]);
再看看我的答案。我对第一个选项进行了编辑,使其正常工作。工作正常。。谢谢
$operations = [
'' => 'Select Operation',
'query' => 'Query',
'create' => 'Create',
'update' => 'Update',
'upsert' => 'Upsert',
'delete' => 'Delete'
];
echo $this->Form->input('operation[]',[
'type' => 'select',
'options' => $operations,
'class' => 'operation-class',
'id' => 'operation-id-1',
'required' => 'required',
'label' => false,
'disabled' => [''],
'value' => ''
]);