From e334c472fb4be44feec3a73402fc4a2b062cbfc0 Mon Sep 17 00:00:00 2001
From: admin If you submit the form you should simply see the form reload. That's because you haven't set up any validation
+ If you submit the form you should simply see the form reload. That's because you haven't set up any validation
rules yet, which we'll get to in a moment. The controller (form.php) has one function: index(). This function initializes the validation class and
-loads the form helper and URL helper used by your view files. It also runs
-the validation routine. Based on
+loads the form helper and URL helper used by your view files. It also runs
+the validation routine. Based on
whether the validation was successful it either presents the form or the success page. Since you haven't told the validation class to validate anything yet, it returns "false" (boolean false) by default. The run()
@@ -313,7 +313,7 @@ $rules['email'] = "required|valid_email";
In addition to the validation functions like the ones we used above, you can also prep your data in various ways.
+ In addition to the validation functions like the ones we used above, you can also prep your data in various ways.
For example, you can set up rules like this:
To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix. The error message was set using the $this->validation->set_message function.
+ The error message was set using the $this->validation->set_message function.
Just remember that the message key (the first parameter) must match your function name. Note: You can apply your own custom error messages to any rule, just by setting the
+ Note: You can apply your own custom error messages to any rule, just by setting the
message similarly. For example, to change the message for the "required" rule you will do this: Thus far we have only been dealing with errors. It's time to repopulate the form field with the submitted data.
+ Thus far we have only been dealing with errors. It's time to repopulate the form field with the submitted data.
This is done similarly to your rules. Add the following code to your controller, just below your rules: The array keys are the actual names of the form fields, the value represents the full name that you want shown in the
+ The array keys are the actual names of the form fields, the value represents the full name that you want shown in the
error message. The index function of your controller should now look like this:
www.your-site.com/index.php/form/
-Prepping Data
-$rules['username'] = "trim|required|min_length[5]|max_length[12]|xss_clean";
@@ -391,17 +391,17 @@ callback function for you to process.$this->validation->set_message('required', 'Your custom message here');
Re-populating the form
-$fields['username'] = 'Username';
-
@@ -411,7 +411,7 @@ $fields['email'] = 'Email Address';
$this->validation->set_fields($fields);
Note: To display errors this way you must remember to set your fields using the $this->validation->set_fields
-function described earlier. The errors will be turned into variables that have "_error" after your field name.
+function described earlier. The errors will be turned into variables that have "_error" after your field name.
For example, your "username" error will be available at:
$this->validation->username_error.
If you use select menus, radio buttons or checkboxes, you will want the state of +
If you use select menus, radio buttons or checkboxes, you will want the state of these items to be retained in the event of an error. The Validation class has three functions that help you do this: