Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: dropdown validation set_rules

may i know how do i ensure that the user have selected, Dr, Mr, Ms, Mdm and when they submit the form if the salutation is given as blank it will return an error message for the set_rules().

Code:

echo "<p>Salutation: ";
    $salutationOptions = array(
                  ''  => '',
                  'Dr'  => 'Dr',
                  'Mr'    => 'Mr',
                  'Ms'   => 'Ms',
                  'Mdm' => 'Mdm',
                );
    echo form_dropdown('salutation', $salutationOptions, '');
    echo "</p>";
like image 811
Outliers Avatar asked Sep 28 '22 22:09

Outliers


1 Answers

In the view file, you can do a client side validation using this:

echo "<p>Salutation: ";
 $salutationOptions = array(
                  ''  => '',
                  'Dr'  => 'Dr',
                  'Mr'    => 'Mr',
                  'Ms'   => 'Ms',
                  'Mdm' => 'Mdm',
                );
    echo form_dropdown('salutation', $salutationOptions, '', 'required="required"');
    echo "</p>";

When the user tries to submit without choosing form the dropdown, it will give them an error saying they should choose from the dropdown.

If you want it on the server side, you can do something like this:

$this->form_validation->set_rules('salutation', 'Salutation', 'required')
if($this->form_validation->run()){
    /*user has selected. use the data now*/
}else{
    /*user has not sleected data, throw error back*/
}
like image 181
Adr Avatar answered Oct 03 '22 02:10

Adr