From 66bc2ad56cd7dcaab7f07d5357e25ea577460a8e Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Tue, 26 Aug 2008 20:18:17 +0000 Subject: --- user_guide/libraries/form_validation.html | 59 +++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'user_guide') diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html index 6ed0b1ded..3da5c31f4 100644 --- a/user_guide/libraries/form_validation.html +++ b/user_guide/libraries/form_validation.html @@ -79,6 +79,7 @@ have left the old class in the library so applications currently using it will n
  • Changing the Error Delimiters
  • Showing Errors Individually
  • Saving Sets of Validation Rules to a Config File
  • +
  • Using Arrays as Field Names
  • Rule Reference
  • @@ -830,9 +831,63 @@ class Member extends Controller {

    When a rule group is named identically to a controller class/function it will be used automatically when the run() function is invoked from that class/function.

    +

     

    + + + +

    Using Arrays as Field Names

    + +

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

    + +<input type="text" name="options[]" value="" size="50" /> + +

    If you do use an array as a field name, you must use the EXACT array name in the Helper Functions that require the field name, +and as your Validation Rule field name.

    + +

    For example, to set a rule for the above field you would use:

    + +$this->form_validation->set_rules('options[]', 'Options', 'required'); + +

    Or, to show an error for the above field you would use:

    + +<?php echo form_error('options[]'); ?> + +

    Or to re-populate the field you would use:

    + +<input type="text" name="options[]" value="<?php echo set_value('options[]'); ?>" size="50" /> + +

    You can use multidimensional arrays as field names as well. For example:

    +<input type="text" name="options[size]" value="" size="50" /> + +

    Or even:

    + +<input type="text" name="sports[nba][basketball]" value="" size="50" /> + +

    As with our first example, you must use the exact array name in the helper functions:

    + +<?php echo form_error('sports[nba][basketball]'); ?> + +

    If you are using checkboxes (or other fields) that have multiple options, don't forget to leave an empty bracket after each option, so that all selections will be added to the +POST array:

    + + +<input type="checkbox" name="options[]" value="red" />
    +<input type="checkbox" name="options[]" value="blue" />
    +<input type="checkbox" name="options[]" value="green" /> +
    + +

    Or if you use a multidimensional array:

    + + +<input type="checkbox" name="options[color][]" value="red" />
    +<input type="checkbox" name="options[color][]" value="blue" />
    +<input type="checkbox" name="options[color][]" value="green" /> +
    +

    When you use a helper function you'll include the bracket as well:

    +<?php echo form_error('options[color][]'); ?> @@ -1108,8 +1163,8 @@ each item, and the third (optional) parameter lets you set an item as the defaul

    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'); ?> />
    -<input type="checkbox" name="mycheck" value="2" <?php echo set_checkbox('mycheck', '2'); ?> />
    +<input type="checkbox" name="mycheck[]" value="1" <?php echo set_checkbox('mycheck[]', '1'); ?> />
    +<input type="checkbox" name="mycheck[]" value="2" <?php echo set_checkbox('mycheck[]', '2'); ?> />

    set_radio()

    -- cgit v1.2.3-24-g4f1b