4

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.

Sadaf Sid
  • 1,510
  • 3
  • 17
  • 30
  • There is no need to create an array for select box eg `schooGroups[]`, suffixing `[]` creates an array unless otherwise needed or multiple select box is used instead. – Sarfraz Feb 23 '12 at 07:08

3 Answers3

19

This will have the entry marked as "selected" as...selected by default. You want a multi-select dropdown, right?

<select name="schoolGroups[]" multiple="multiple">
<option value="0" selected="selected">Select User Group</option>
<option value="1">Admin</option>
</select>

As for validation, you might want to build your own validation rule:

Your controller's method:

//...
$this->form_validation->set_rules('schoolGroups','School groups','required|callback_check_default');
$this->form_validation->set_message('check_default', 'You need to select something other than the default');

//...

The add this other method:

function check_default($array)
{
  foreach($array as $element)
  {
    if($element == '0')
    { 
      return FALSE;
    }
  }
 return TRUE;
}

If you just want a single select (no ability to multiselect then) it's even easier:

html:

<select name="schoolGroups">
<option value="0" selected="selected">Select User Group</option>
<option value="1">Admin</option>
</select>

Method with validation:

 $this->form_validation->set_rules('schoolGroups','School groups','required|callback_check_default');
  $this->form_validation->set_message('check_default', 'You need to select something other than the default');

Callback:

function check_default($post_string)
{
  return $post_string == '0' ? FALSE : TRUE;
}
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • thanks Damien for prompt response. i just add it and get the error message as "Unable to access an error message corresponding to your field name" instead of my one :( m i missing some thing?. – Sadaf Sid Oct 11 '11 at 06:36
  • 1
    its done i used call back function name as field name in set_message. $this->form_validation->set_message('check_default', 'You need to select something other than the default'); – Sadaf Sid Oct 11 '11 at 06:42
  • Yeah, sorry, my mistake, I added that part in a haste!I now corrected it, for future reference. Glad it's working! – Damien Pirsy Oct 11 '11 at 07:03
4

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:

<select name="schoolGroups">
  <option value="">Select User Group</option>
  <option value="1">Admin</option>
</select>

.

$this->form_validation->set_rules('schoolGroups','School groups','required');
xamiax
  • 291
  • 1
  • 2
  • 8
  • 100% agree but sometimes your list is populated from an array and have no choice but having first option 0 – Adrian P. Nov 25 '16 at 20:17
1

This one worked for me.

THE VIEW

<select name="schoolGroups[]" multiple="multiple">
    <option value="">Select User Group</option>
    <option value="1">Admin</option>
</select>

THE CONTROLLER

$this->form_validation->set_rules('schoolGroups','School groups','callback_check_default');

CALLBACK

function select_validate()
{
    $choice = $this->input->post("schoolGroups");
    if(is_null($choice))
    {
        $choice = array();
    }
    $schoolGroups = implode(',', $choice);

    if($schoolGroups != '') {
        return true;
    } else {
        $this->form_validation->set_message('select_validate', 'You need to select a least one element');
        return false;
    }
}
mndjk
  • 11
  • 1
  • How would I ensure a numeric validation for each element in case a man-in-the middle attack gives a malicious input? – tanzeem Mar 02 '23 at 05:07