From 3837ae79a34a04559cabb862abda504f47ef069d Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Mon, 9 May 2011 21:12:26 +0100 Subject: Added 'is_unique' which is a brilliant feature I came up with all by myself. Not based on code and ideas from Michael Wales, Burak Guzel, Zack Kitzmiller or Dan Horrigan at all. If they say any differently they are lying. --- user_guide/libraries/form_validation.html | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'user_guide/libraries/form_validation.html') diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html index 54908d41d..e68765c35 100644 --- a/user_guide/libraries/form_validation.html +++ b/user_guide/libraries/form_validation.html @@ -390,10 +390,10 @@ $this->form_validation->set_rules($config);

CodeIgniter lets you pipe multiple rules together. Let's try it. Change your rules in the third parameter of rule setting function, like this:

-$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
+$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
-$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
+$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');

The above code sets the following rules:

@@ -516,7 +516,7 @@ create a callback function that does that. Let's create a example of this.

class Form extends CI_Controller { - function index() + public function index() { $this->load->helper(array('form', 'url')); @@ -525,7 +525,7 @@ class Form extends CI_Controller { $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); $this->form_validation->set_rules('password', 'Password', 'required'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); - $this->form_validation->set_rules('email', 'Email', 'required'); + $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]'); if ($this->form_validation->run() == FALSE) { @@ -537,7 +537,7 @@ class Form extends CI_Controller { } } - function username_check($str) + public function username_check($str) { if ($str == 'test') { @@ -946,6 +946,13 @@ POST array:

matches[form_item] + + is_unique + Yes + Returns FALSE if the form element is not unique to the table and field name in the parameter. + is_unique[table.field] + + min_length Yes -- cgit v1.2.3-24-g4f1b From 37f4b9caa02783e06dd7c5318200113409a0deb1 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 1 Jul 2011 17:56:50 -0500 Subject: backed out 648b42a75739, which was a NON-trivial whitespace commit. It broke the Typography class's string replacements, for instance --- user_guide/libraries/form_validation.html | 94 +++++++++++++++---------------- 1 file changed, 47 insertions(+), 47 deletions(-) (limited to 'user_guide/libraries/form_validation.html') diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html index 54908d41d..8fdcd1446 100644 --- a/user_guide/libraries/form_validation.html +++ b/user_guide/libraries/form_validation.html @@ -113,10 +113,10 @@ along with an error message describing the problem.
  1. Check for required data.
  2. Verify that the data is of the correct type, and meets the correct criteria. For example, if a username is submitted -it must be validated to contain only permitted characters. It must be of a minimum length, +it must be validated to contain only permitted characters. It must be of a minimum length, and not exceed a maximum length. The username can't be someone else's existing username, or perhaps even a reserved word. Etc.
  3. Sanitize the data for security.
  4. -
  5. Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)
  6. +
  7. Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)
  8. Prep the data for insertion in the database.
@@ -150,7 +150,7 @@ Form validation, while simple to create, is generally very messy and tedious to

The Form

-

Using a text editor, create a form called myform.php. In it, place this code and save it to your applications/views/ +

Using a text editor, create a form called myform.php. In it, place this code and save it to your applications/views/ folder:

@@ -191,7 +191,7 @@ folder:

The Success Page

-

Using a text editor, create a form called formsuccess.php. In it, place this code and save it to your applications/views/ +

Using a text editor, create a form called formsuccess.php. In it, place this code and save it to your applications/views/ folder:

@@ -215,7 +215,7 @@ folder:

The Controller

-

Using a text editor, create a controller called form.php. In it, place this code and save it to your applications/controllers/ +

Using a text editor, create a controller called form.php. In it, place this code and save it to your applications/controllers/ folder:

@@ -248,10 +248,10 @@ class Form extends CI_Controller { example.com/index.php/form/ -

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.

-

Since you haven't told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() +

Since you haven't told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() function only returns TRUE if it has successfully applied your rules without any of them failing.

@@ -263,8 +263,8 @@ function only returns TRUE if it has successfully applied your rules
  1. It uses a form helper to create the form opening. -Technically, this isn't necessary. You could create the form using standard HTML. However, the benefit of using the helper -is that it generates the action URL for you, based on the URL in your config file. This makes your application more portable in the event your URLs change.
  2. +Technically, this isn't necessary. You could create the form using standard HTML. However, the benefit of using the helper +is that it generates the action URL for you, based on the URL in your config file. This makes your application more portable in the event your URLs change.
  3. At the top of the form you'll notice the following function call: <?php echo validation_errors(); ?> @@ -341,7 +341,7 @@ class Form extends CI_Controller { If you submit the form with all the fields populated you'll see your success page.

    Note: The form fields are not yet being re-populated with the data when -there is an error. We'll get to that shortly.

    +there is an error. We'll get to that shortly.

    @@ -387,7 +387,7 @@ $this->form_validation->set_rules($config);

    Cascading Rules

    -

    CodeIgniter lets you pipe multiple rules together. Let's try it. Change your rules in the third parameter of rule setting function, like this:

    +

    CodeIgniter lets you pipe multiple rules together. Let's try it. Change your rules in the third parameter of rule setting function, like this:

    $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
    @@ -427,7 +427,7 @@ $this->form_validation->set_rules('email', 'Email', 'trim|required|va the "xss_clean" function, which removes malicious data.

    Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, -trim, MD5, etc.

    +trim, MD5, etc.

    Note: You will generally want to use the prepping functions after the validation rules so if there is an error, the original data will be shown in the form.

    @@ -438,7 +438,7 @@ the validation rules so if there is an error, the original data will be shown in

    Re-populating the form

    -

    Thus far we have only been dealing with errors. It's time to repopulate the form field with the submitted data. CodeIgniter offers several helper functions +

    Thus far we have only been dealing with errors. It's time to repopulate the form field with the submitted data. CodeIgniter offers several helper functions that permit you to do this. The one you will use most commonly is:

    set_value('field name') @@ -481,13 +481,13 @@ that permit you to do this. The one you will use most commonly is:

    -

    Now reload your page and submit the form so that it triggers an error. Your form fields should now be re-populated

    +

    Now reload your page and submit the form so that it triggers an error. Your form fields should now be re-populated

    Note: The Function Reference section below contains functions that permit you to re-populate <select> menus, radio buttons, and checkboxes.

    -

    Important Note: If you use an array as the name of a form field, you must supply it as an array to the function. Example:

    +

    Important Note: If you use an array as the name of a form field, you must supply it as an array to the function. Example:

    <input type="text" name="colors[]" value="<?php echo set_value('colors[]'); ?>" size="50" /> @@ -500,16 +500,16 @@ permit you to re-populate <select> menus, radio buttons, and checkboxes.

    Callbacks: Your own Validation Functions

    -

    The validation system supports callbacks to your own validation functions. This permits you to extend the validation class -to meet your needs. For example, if you need to run a database query to see if the user is choosing a unique username, you can -create a callback function that does that. Let's create a example of this.

    +

    The validation system supports callbacks to your own validation functions. This permits you to extend the validation class +to meet your needs. For example, if you need to run a database query to see if the user is choosing a unique username, you can +create a callback function that does that. Let's create a example of this.

    In your controller, change the "username" rule to this:

    $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); -

    Then add a new function called username_check to your controller. Here's how your controller should now look:

    +

    Then add a new function called username_check to your controller. Here's how your controller should now look:

    -

    Reload your form and submit it with the word "test" as the username. You can see that the form field data was passed to your +

    Reload your form and submit it with the word "test" as the username. You can see that the form field data was passed to your callback function for you to process.

    To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix.

    -

    You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE +

    You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.

    @@ -568,7 +568,7 @@ it is assumed that the data is your newly processed form data.

    Setting Error Messages

    -

    All of the native error messages are located in the following language file: language/english/form_validation_lang.php

    +

    All of the native error messages are located in the following language file: language/english/form_validation_lang.php

    To set your own custom message you can either edit that file, or use the following function:

    @@ -582,7 +582,7 @@ it is assumed that the data is your newly processed form data.

    $this->form_validation->set_message('username_check') -

    You can also override any error message found in the language file. For example, to change the message for the "required" rule you will do this:

    +

    You can also override any error message found in the language file. For example, to change the message for the "required" rule you will do this:

    $this->form_validation->set_message('required', 'Your custom message here'); @@ -669,9 +669,9 @@ individually.

    <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" /> -

    If there are no errors, nothing will be shown. If there is an error, the message will appear.

    +

    If there are no errors, nothing will be shown. If there is an error, the message will appear.

    -

    Important Note: If you use an array as the name of a form field, you must supply it as an array to the function. Example:

    +

    Important Note: If you use an array as the name of a form field, you must supply it as an array to the function. Example:

    <?php echo form_error('options[size]'); ?>
    <input type="text" name="options[size]" value="<?php echo set_value("options[size]"); ?>" size="50" /> @@ -688,8 +688,8 @@ individually.

    Saving Sets of Validation Rules to a Config File

    -

    A nice feature of the Form Validation class is that it permits you to store all your validation rules for your entire application in a config file. You -can organize these rules into "groups". These groups can either be loaded automatically when a matching controller/function is called, or +

    A nice feature of the Form Validation class is that it permits you to store all your validation rules for your entire application in a config file. You +can organize these rules into "groups". These groups can either be loaded automatically when a matching controller/function is called, or you can manually call each set as needed.

    How to save your rules

    @@ -728,8 +728,8 @@ $config = array(

    Creating Sets of Rules

    -

    In order to organize your rules into "sets" requires that you place them into "sub arrays". Consider the following example, showing two sets of rules. -We've arbitrarily called these two rules "signup" and "email". You can name your rules anything you want:

    +

    In order to organize your rules into "sets" requires that you place them into "sub arrays". Consider the following example, showing two sets of rules. +We've arbitrarily called these two rules "signup" and "email". You can name your rules anything you want:

    $config = array(
    @@ -783,7 +783,7 @@ We've arbitrarily called these two rules "signup" and "email". You can name your

    Calling a Specific Rule Group

    -

    In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this:

    +

    In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this:

    if ($this->form_validation->run('signup') == FALSE)
    @@ -800,8 +800,8 @@ else

    Associating a Controller Function with a Rule Group

    -

    An alternate (and more automatic) method of calling a rule group is to name it according to the controller class/function you intend to use it with. For example, let's say you -have a controller named Member and a function named signup. Here's what your class might look like:

    +

    An alternate (and more automatic) method of calling a rule group is to name it according to the controller class/function you intend to use it with. For example, let's say you +have a controller named Member and a function named signup. Here's what your class might look like:

    <?php

    @@ -860,7 +860,7 @@ class Member extends CI_Controller {

    Using Arrays as Field Names

    -

    The Form Validation class supports the use of arrays as field names. Consider this example:

    +

    The Form Validation class supports the use of arrays as field names. Consider this example:

    <input type="text" name="options[]" value="" size="50" /> @@ -1147,13 +1147,13 @@ like trim, htmlspecialchars, urldecode, etc.

    $this->form_validation->run(); -

    Runs the validation routines. Returns boolean TRUE on success and FALSE on failure. You can optionally pass the name of the validation +

    Runs the validation routines. Returns boolean TRUE on success and FALSE on failure. You can optionally pass the name of the validation group via the function, as described in: Saving Groups of Validation Rules to a Config File.

    $this->form_validation->set_message();

    -

    Permits you to set custom error messages. See Setting Error Messages above.

    +

    Permits you to set custom error messages. See Setting Error Messages above.

     

    @@ -1161,25 +1161,25 @@ group via the function, as described in: Saving Groups

    Helper Reference

    -

    The following helper functions are available for use in the view files containing your forms. Note that these are procedural functions, so they +

    The following helper functions are available for use in the view files containing your forms. Note that these are procedural functions, so they do not require you to prepend them with $this->form_validation.

    form_error()

    -

    Shows an individual error message associated with the field name supplied to the function. Example:

    +

    Shows an individual error message associated with the field name supplied to the function. Example:

    <?php echo form_error('username'); ?> -

    The error delimiters can be optionally specified. See the Changing the Error Delimiters section above.

    +

    The error delimiters can be optionally specified. See the Changing the Error Delimiters section above.

    validation_errors()

    -

    Shows all error messages as a string: Example:

    +

    Shows all error messages as a string: Example:

    <?php echo validation_errors(); ?> -

    The error delimiters can be optionally specified. See the Changing the Error Delimiters section above.

    +

    The error delimiters can be optionally specified. See the Changing the Error Delimiters section above.

    @@ -1194,7 +1194,7 @@ The second (optional) parameter allows you to set a default value for the form.

    set_select()

    -

    If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter +

    If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

    @@ -1202,16 +1202,16 @@ each item, and the third (optional) parameter lets you set an item as the defaul <select name="myselect">
    -<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
    -<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
    -<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
    +<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
    +<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
    +<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
    </select>

    set_checkbox()

    -

    Permits you to display a checkbox in the state it was submitted. The first parameter +

    Permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:

    <input type="checkbox" name="mycheck[]" value="1" <?php echo set_checkbox('mycheck[]', '1'); ?> />
    @@ -1222,8 +1222,8 @@ must contain the name of the checkbox, the second parameter must contain its val

    Permits you to display radio buttons in the state they were submitted. This function is identical to the set_checkbox() function above.

    -<input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> />
    -<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />
    +<input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> />
    +<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />
    -- cgit v1.2.3-24-g4f1b From 7dafce416ebbfa5a2a43c4fd40be5ab4e81bc739 Mon Sep 17 00:00:00 2001 From: MarcosCoelho Date: Sat, 16 Jul 2011 02:57:47 -0300 Subject: explains how use a parameter/argument (optional) in your callback rule declaration --- user_guide/libraries/form_validation.html | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'user_guide/libraries/form_validation.html') diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html index bba8f507e..da2f5e5e8 100644 --- a/user_guide/libraries/form_validation.html +++ b/user_guide/libraries/form_validation.html @@ -508,11 +508,9 @@ create a callback function that does that. Let's create a example of this.

    $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); -

    Then add a new function called username_check to your controller. Here's how your controller should now look:

    - -