我正在创建一个表单:
<?= $form->field($model, 'job_category')
->dropDownList($jobCategoryDropDown, ['options' => $jobCategoryOptionArray, 'multiple' => 'multiple'])
->label('Ngành nghề - chuyên môn (Bắt buộc) <span class="help-required"> * </span>') ?>
如何限制用户选择最多3个或更多值?
您可以这样进行。在模型中声明一个验证器函数。
public function limitSelection($attribute,$params)
{
if(count($this->$attribute)>3)
{
$this->addError($attribute,"You are only allowed to select 3 or less items for ".$attribute);
}
}
然后以以下方式声明规则。
public function rules()
{
return [
..................other rules......................
['job_category', 'required','message'=>"Select at least one item for {attribute}"],
['job_category', 'limitSelection']
];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句