As I anticipated in the introduction, the validation class that comes included with Code Igniter permits us to apply severe checking rules for the data entered into a specific online form. However, as with each application developed with this framework, it’s necessary to build a controller class that actually performs this validation process. Then, if the form fails to pass the checking procedure, the controller will redisplay it, along with a descriptive error message. Otherwise, a confirmation web page will be shown instead. Fairly easy to follow, right? Now that you have grasped the logic that will be implemented by the pertinent controller class, let me show you how it looks. 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|min_length[6]|max_length[15]"; $rules['lastname']="required|min_length[6]|max_length[15]"; $rules['email']="required|valid_email"; $this->validation->set_rules($rules); // set values for repopulating fields $fields['firstname']='First Name'; $fields['lastname']= 'Last Name'; $fields['email']='Email Address'; $this->validation->set_fields($fields); // check if user form has been submitted properly if ($this->validation->run()==FALSE){ // redisplay user form and repopulate fields $this->load->view('webform_view'); } // display success web page else{ $this->load->view('success_view'); } } } Regardless of its short signature, the above “Validator” controller class does many useful things that need to be explained in more detail. First, the constructor loads the corresponding validation class, and the “url” and “form” helpers that you learned about in previous tutorials. As you can see, understanding how this process is achieved is pretty simple. But the most interesting things happen when the “index()” method is called. As shown before, it first sets specifically the validation rules that will be applied to each field of the sample web form, via its “set_rules()” method, and additionally, specifies the group of values that will be used to repopulate the HTML form, if it’s submitted incorrectly. Finally, using the previously defined validation rules, the web form is properly checked, and in accordance with the result of this operation, the controller either will redisplay the online form with its fields repopulated, or it’ll simply show a confirmation web page. At this point, are you starting to realize how easy it is to perform strict validation on a selected web form by using Code Igniter? I guess you are! However, if you’re anything like me, then I’m sure you’ll want to see how each of the respective views are created, right? So, save the previous controller class to the /system/application/controllers/ folder as “validator.php.” In this way you can test it with your own web server. Assuming that you’ve already done this, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|