I am creating a form with this:
<?= $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>') ?>
How can I limit users to select a maximum of 3 or more values?
You can do it this way. Declare a validator function in the model.
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);
}
}
Then declare the rules in the following way.
public function rules()
{
return [
..................other rules......................
['job_category', 'required','message'=>"Select at least one item for {attribute}"],
['job_category', 'limitSelection']
];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.