How to implement validations in Codeigniter?

devquora
devquora

Posted On: Apr 25, 2024

 

    1 Answer Written

  •  devquora
    Answered by Christopher

    You can implement form validations in Codeigniter using a form_validation library. Below is simple server-side validation in Codeigniter.

    In your Controller

             $this->load->helper(array('form'));			
             /* Load form validation library */ 
             $this->load->library('form_validation');			
             /* Set validation rule for name field in the form */ 
             $this->form_validation->set_rules('firstname', 'FirstName', 'required'); 
    			
             if ($this->form_validation->run() == FALSE) { 
                  $this->load->view('myform'); 
             } 
             else { 
                $this->load->view('formsuccess'); 
             } 
    
    

    In your View

     <?php echo validation_errors(); ?> 
    

    With the help of Codeigniter Validation libraries, you can easily set many validation rules according to the needs of your given field. You can assemble them in an order and the field data is also processed throughout the same time. To make sure that when a user enters data, it needs to be checked to be authentic there is a need for the validation of the form. If you have date values to be entered, you would want to make sure that the values are correctly listed. In the same way, if there is data of email ID, you would want correct email Ids submitted through the forms.

    Client-Side validation is done with the help of the web browser. HTML and JavaScript are used here. The User has complete control over the working. If the user fails to enable or does disable of the JavaScript, then the validation may fail.

    Server-side validation is another possibility wherein once the user submits the data it is processed and the user needs to wait till processing is complete. Network Resources are used up here. Even if JavaScript is disabled by the User, your validation will happen for sure.

Related Questions

Please Login or Register to leave a response.

Related Questions

CodeIgniter Interview Questions

How to check the version of CodeIgniter framework?

How to check the version of CodeIgniter framework?..