[英]Yii2 ActiveForm - dropDownList make template for {input}
[英]How can I set max selection for Yii2 ActiveForm dropDownList?
我正在用這個創建一個表單:
<?= $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']
];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.