要在Yii框架2.0中生成一个复选框列表,我使用以下代码行:
Html::checkboxList($model->choises, null, $arrayOption, ['separator' => '<br />',]);
我想有一个像others
输入文本字段的最后一个复选框选项。Yii框架2.0提供任何视图帮助器来生成它吗?
由于我需要接近这个目标,我尝试了不同的解决方案,并提出了一个理想的解决方案,我想在这里分享,因为还没有人能回答这个问题。对radionList
和checkboxList
都有解
只需使用以下代码片段注入$arrayOption
。
$arrayOption[0] = 'Others <input type="text" class="my-class-name" name="MyModel[my_other_option]">';
将以下代码添加到model
类内部的rules
函数中。
[
['my_other_option',], 'required' , 'when' => function($model) {
return $model->choises == '0';
},
],
通过这样做,当您选择了Other
选项但没有填写名为my_other_option
的输入文本字段时,验证将失败。