As I mentioned in the introduction, validating web forms with Code Igniter is actually a no-brainer process that can be tackled by any developer with an intermediate background in the Model-View-Controller pattern. To be frank, the whole validation procedure is reduced to creating a controller class that checks to see if the data collected with an online form is valid or not, and then shows one of two different views, according to the result of this checking process. Obviously, the first view will display a successful web page, while the second one will show an error message along with the corresponding web form. Quite simple to understand, right? However, the best way to grasp how to validate online forms with Code Igniter is simply by showing some functional code. Period. Thus, based on this concept, below I listed the signature of a brand new controller class which performs all the validation steps described previously. Here it is: class Validator extends Controller { function Validator(){ // load controller parent parent::Controller(); // load 'url' helper $this->load->helper('url'); // load 'form' helper $this->load->helper('form'); // load 'validation' class $this->load->library('validation'); } function index(){ // set validation rules $rules['firstname']="required"; $rules['lastname']="required"; $rules['email']="required"; $this->validation->set_rules($rules); // check if form has been submitted properly if ($this->validation->run()==FALSE){ // redisplay web form $this->load->view('form_view'); } // display success web page else{ $this->load->view('success_view'); } } } It’s hard to believe, but the above “Validator” controller class is almost all the PHP code required to validate a web form. In this specific case, the controller begins loading all of the classes that it’ll need to perform a basic validation on a sample contact form. The form is composed of some typical fields, such as the user’s first and last names, and the email address. As you can see, the class includes a couple of helper functions, called “url” and “form” respectively. Not surprisingly, these functions are used by Code Igniter to generate dynamic URLs and form parts too. However, these functions will be utilized at a later time, when rendering the corresponding views. Thus, for the moment, pay attention to the following line of code: $this->load->library('validation'); As with other examples developed earlier, this expression is used to include the validation class, which naturally is utilized to check whether or not the fields of the sample web form have been filled with data. This process can be more clearly understood if you examine the code snippet below: // set validation rules $rules['firstname']="required"; $rules['lastname']="required"; $rules['email']="required"; $this->validation->set_rules($rules); In this concrete situation, the set of checking rules assigned to the validation class are pretty simple actually, since they’ll only verify if the text boxes of the web form aren’t empty. That’s all. However, it’s perfectly possible to specify more strict validation rules, but this topic will be covered in detail in upcoming articles of the series. Once the controller determines if the web form has been submitted correctly (or not), it’ll redisplay the form, including an error message, or it’ll show a success web page, depending on the result of this validation process. I told you that validating online forms with Code Igniter was really easy! And before I forget, please save this file to the Code Igniter /system/application/controllers/ folder as “validator.php.” Now that you have hopefully grasped how the previous “Validator” class works, it’s time to see how to create the view file that redisplays the web form when it contains one or more empty fields. To see how this file will be built, please jump ahead and read the following section.
blog comments powered by Disqus |
|
|
|
|
|
|
|