Codeigniter: set_rules dropdown check

Can I find out how I can make sure that the user has selected Dr, Mr, Ms, Mdm and when they submit the form, if the greeting is given as empty, it will return an error message for set_rules ().

the code:

echo "<p>Salutation: "; $salutationOptions = array( '' => '', 'Dr' => 'Dr', 'Mr' => 'Mr', 'Ms' => 'Ms', 'Mdm' => 'Mdm', ); echo form_dropdown('salutation', $salutationOptions, ''); echo "</p>"; 
+5
source share
4 answers

In the view file, you can perform client-side validation using the following command:

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

When a user tries to send without selecting the form of the drop-down list, he will give them an error saying that they should choose from the drop-down list.

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*/ } 
+4
source

Use required in HTML or

do it

  $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

0
source

An example in sight:

  $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 the 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');} 
0
source

You better go with a foreach () loop. This is the way 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 } ?> 
0
source

Source: https://habr.com/ru/post/1211971/


All Articles