i am new to Codeigniter and I have some trouble in select box validation. I want default select value at beginning.
<select name="schooGroups[]">
<option value="0">Select User Group</option>
<option value="1">Admin</option>
</select>
how can i make it required field in form and display error message on select of zero "0" value.
The correct way of doing this is by setting the value of the default option to empty! Then you can use a simple form_validation rule like for a text field:
.
This will have the entry marked as "selected" as...selected by default. You want a multi-select dropdown, right?
As for validation, you might want to build your own validation rule:
Your controller's method:
The add this other method:
If you just want a single select (no ability to multiselect then) it's even easier:
html:
Method with validation:
Callback:
This one worked for me.
THE VIEW
THE CONTROLLER
CALLBACK