Implementing Codeigniter Form Validation Using Core Controller Function

 Implementing Codeigniter Form Validation Using Core Controller Function


Form validation is an important aspect of website design. In fact, validation is considered more important than the actual form design for a simple reason – a website could survive a badly designed form but a badly implemented validation could potentially damage the business and website operations.

This is why Codeigniter form validation is an important reason behind the popularity of the framework. It is important to understand the ease with which developers could implement validation for all the forms used in the project.

Forms give way for clients to interact with the application and submit data. It can be used for a contact us form that guests can use to fill in and send the data to the user. The data received is usually stored within the database or sent via mail.

In this Codeigniter form validation example, I will highlight the ease with which you can set up form validation through a Controller function. You will see how to alert the users with error messages and configure the relevant route/library in your app deployed on any web hosting with PHP and MySQL support.

Codeigniter Form Validation Controller Function

Here is the basic function for form validation:

  1. class Cwformvalidation extends CI_Controller {
  2. public function index() {
  3. $this->load->helper(array('cwformvalidation'));
  4. $this->load->library('form_validation');
  5. $this->form_validation->set_rules('first field', 'First Field', 'required');
  6. $this->form_validation->set_rules('second field', 'Seconds Field', 'required');
  7. if ($this->form_validation->run() == FALSE) {
  8. $this->load->view('myform');
  9. }
  10. else {
  11. $this->load->view('formsuccess');
  12. }
  13. }
  14. }

helper(array(‘cwformvalidation’))  Load  helper which is cwformvalidation

library(‘form_validation’)   Load  validation library which is cwformvalidation

form_validation->set_rules  set validation rules

Once the user clicks the Submit button, the Controller will load the library and execute the validation rules to ensure that the information submitted by the user fit the validation rules.

In the above function, the helper function loads the validation library. Form validation rules are added in form_validation.

Validation Class

Codeigniter form validation is also implemented by a controller function that can be called by the default helper function. You can execute the following code to test this method:

  1. class Validation extends CI_Controller {
  2. public function __construct()
  3. {
  4. parent::__construct();
  5. $this->load->helper(array('form', 'url'));
  6. $this->load->library('form_validation');
  7. }
  8. }

In the above code snippet, the helper function loads the form. The good part about the above snippet is that you can easily add custom validation messages by adding the following line:

  1. $this->form_validation->set_rules('alphabets_text_field', 'field name', 'required|alpha',
  2. array('required'=>'your custom message'));

If you need to customize input field validation messages, add the following function to the controller.

  1. public function message_customization()
  2. {
  3. $this->form_validation->set_rules('filed_name', 'Filed Name', 'required',
  4. array('required'=>'your custom error message!'));
  5. if ($this->form_validation->run() == FALSE)
  6. {
  7. $this->load->view('formsuccess');
  8. }
  9. else
  10. {
  11. echo "Success Message";
  12. }
  13. }

Note that in the above function, in the line $this->load->view(‘formsuccess’), you will notice that I have loaded a view named ‘formsuccess’, . you can name this view anything you wish.

Build the View

The View contains the frontend elements that you can use to render the form and add the validation. Add the following code snippet to the view:

  1. <form action = " " method = "">
  2. <?php echo validation_errors(); ?>
  3. <?php echo form_open('form'); ?>
  4. <h5>First field</h5>
  5. <input type = "text" name = "first field" value = "" />
  6. <h5>Second field field</h5>
  7. <input type = "text" name = "second field" value = "" />
  8. <div><input type = "submit" value = "Submit" /></div>
  9. </form>

The above code snippet performs AJAX based CodeIgniter form validation. If you wish, you can add AJAX based form submission and validation. Add the following code snippet if you wish to add customization of form field values:

  1. if ($this->form_validation->run() == FALSE) {
  2. $this->load->view('your-view-of-susses-message');
  3. }
  4. else {
  5. $this->load->view('formsuccess');
  6. }

Conclusion

Now that you have come to the end of this article, you know how to add Codeigniter form validation using Codeigniter validation library and helper functions. I also demonstrated the use of custom error messages. If you need help with CI form validation, do leave a comment below and I will help you out.

Comments