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:
- class Cwformvalidation extends CI_Controller {
- public function index() {
- $this->load->helper(array('cwformvalidation'));
- $this->load->library('form_validation');
- $this->form_validation->set_rules('first field', 'First Field', 'required');
- $this->form_validation->set_rules('second field', 'Seconds Field', 'required');
- if ($this->form_validation->run() == FALSE) {
- $this->load->view('myform');
- }
- else {
- $this->load->view('formsuccess');
- }
- }
- }
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:
- class Validation extends CI_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->helper(array('form', 'url'));
- $this->load->library('form_validation');
- }
- }
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:
- $this->form_validation->set_rules('alphabets_text_field', 'field name', 'required|alpha',
- array('required'=>'your custom message'));
If you need to customize input field validation messages, add the following function to the controller.
- public function message_customization()
- {
- $this->form_validation->set_rules('filed_name', 'Filed Name', 'required',
- array('required'=>'your custom error message!'));
- if ($this->form_validation->run() == FALSE)
- {
- $this->load->view('formsuccess');
- }
- else
- {
- echo "Success Message";
- }
- }
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:
- <form action = " " method = "">
- <?php echo validation_errors(); ?>
- <?php echo form_open('form'); ?>
- <h5>First field</h5>
- <input type = "text" name = "first field" value = "" />
- <h5>Second field field</h5>
- <input type = "text" name = "second field" value = "" />
- <div><input type = "submit" value = "Submit" /></div>
- </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:
- if ($this->form_validation->run() == FALSE) {
- $this->load->view('your-view-of-susses-message');
- }
- else {
- $this->load->view('formsuccess');
- }
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
Post a Comment