5

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>";
1

4 Answers 4

4

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*/
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use required in HTML , or

do like this

 $this->form_validation->set_rules( "Salutation","Salutation","required"); 

I hope this might help you

https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

8 Comments

Thank you for the reply however this require me to have all of the options Dr, Mr, Ms and Mdm, but what i need is them to choose at least one of the options either Dr, Mr, Ms or Mdm is it possible?
what is your selection tag name? if it is salutation . do like follow , $this->form_validation->set_rules( "Salutation","Salutation","required"); now this is mandatory field so user has to select any one among them
Yeah it is salutation, however as we can see from my code that i have put above, i would want them to change the value to Dr, Mr, Ms or Mdm and not leave it empty at " ", is everything more clear?:)
why dont you remove that empty option?
Thank you very much for helping! i managed to get it working by using Adr solution. :)
|
0

example in view part:

 $options = array('0' => 'SELECT SHIRT', '1' => 'Small Shirt', '1' =>
'Medium Shirt', '2' => 'Large Shirt', '3' => 'Extra Large Shirt', );
echo form_open('welcome/index');
echo form_error('field name to display error');
echo form_dropdown('field name', $options);
echo form_submit('submit', 'submit');
echo form_close();

in controller part:

$this->load->helper('form_validation');
$this->form_validation->set_rules('field name', 'name have to display in the error message', 'required'); 
if ($this->form_validation->run() == false) {
$this->load->view('your page name'); }else{
$this->load->view('another page');}

1 Comment

Thank you very much for helping! i managed to get it working by using Adr solution. :)
0

Better you would go with foreach() loop. This is the way which I came across.

<?php foreach($classes as $class) { ?>
   <option value="<?php echo $class->classesID; ?>" <?php echo set_select('class', $class->classesID); ?>>
      <?php echo $class->classes; ?>
   </option>
<?php } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.