Roby Sottini
Roby Sottini

Reputation: 2265

Yii2: How to add required attribute to a checkboxList?

I have this checkboxList:

Html::checkboxList('MyOffices', null, $offices);

It works but the user has to select at least one option. So I would like to add the required attribute to checkboxList but it doesn't belongs to the model.

Update:

I tried adding this rule to my model file but it didn't work:

[['MyOffices'], 'requiredValue' => 1, 'message' => 'my test message']

Also I tried adding the required attribute in my view file:

Html::checkboxList('MyOffices', null, $offices, ['required' => true]);

Upvotes: 0

Views: 328

Answers (2)

RolandsR
RolandsR

Reputation: 1

Try: Html::checkboxList('MyOffices', null, $offices, ['required' => true]);

Upvotes: 0

Man87
Man87

Reputation: 131

Try the below code:

['acordul_tc', 'required', 'on' => ['register'], 'requiredValue' => 1, 'message' => 'my test message']

This means that the attribute is required and must be equal to 1, else display error with your message.

Upvotes: 1

Related Questions