diff options
Diffstat (limited to 'user_guide_src/source/libraries/form_validation.rst')
-rw-r--r-- | user_guide_src/source/libraries/form_validation.rst | 257 |
1 files changed, 121 insertions, 136 deletions
diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst index 032cff23b..988d6fa25 100644 --- a/user_guide_src/source/libraries/form_validation.rst +++ b/user_guide_src/source/libraries/form_validation.rst @@ -200,6 +200,7 @@ The above method takes **three** parameters as input: message. For example, if your field is named "user" you might give it a human name of "Username". #. The validation rules for this form field. +#. (optional) Set custom error messages on any rules given for current field. If not provided will use the default one. .. note:: If you would like the field name to be stored in a language file, please see :ref:`translating-field-names`. @@ -225,7 +226,9 @@ Your controller should now look like this:: $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'required'); - $this->form_validation->set_rules('password', 'Password', 'required'); + $this->form_validation->set_rules('password', 'Password', 'required', + array('required' => 'You must provide a %s.') + ); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); $this->form_validation->set_rules('email', 'Email', 'required'); @@ -263,7 +266,10 @@ you use this approach, you must name your array keys as indicated:: array( 'field' => 'password', 'label' => 'Password', - 'rules' => 'required' + 'rules' => 'required', + 'errors' => array( + 'required' => 'You must provide a %s.', + ), ), array( 'field' => 'passconf', @@ -285,7 +291,14 @@ Cascading Rules CodeIgniter lets you pipe multiple rules together. Let's try it. Change your rules in the third parameter of rule setting method, like this:: - $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]'); + $this->form_validation->set_rules( + 'username', 'Username', + 'required|min_length[5]|max_length[12]|is_unique[users.username]', + array( + 'required' => 'You have not provided %s.', + 'is_unique' => 'This %s already exists.' + ) + ); $this->form_validation->set_rules('password', 'Password', 'required'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]'); $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]'); @@ -340,10 +353,10 @@ commonly is:: set_value('field name') Open your myform.php view file and update the **value** in each field -using the ``set_value()`` function: +using the :func:`set_value()` function: -**Don't forget to include each field name in the ``set_value()`` -functions!** +**Don't forget to include each field name in the :func:`set_value()` +function calls!** :: @@ -380,11 +393,11 @@ Now reload your page and submit the form so that it triggers an error. Your form fields should now be re-populated .. note:: The :ref:`class-reference` section below - contains functions that permit you to re-populate <select> menus, + contains methods 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:: 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" /> @@ -469,16 +482,23 @@ Setting Error Messages All of the native error messages are located in the following language file: **system/language/english/form_validation_lang.php** -To set your own custom message you can either edit that file, or use the -following method:: +To set your own global custom message for a rule, you can either +edit that file, or use the following method:: $this->form_validation->set_message('rule', 'Error Message'); +If you need to set a custom error message for a particular field on +some particular rule, use the set_rules() method:: + + $this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3', + array('rule2' => 'Error Message on rule2 for this field_name') + ); + Where rule corresponds to the name of a particular rule, and Error Message is the text you would like displayed. -If you'd like to include a field's "human" name, or the optional -parameter some rules allow for (such as max_length), you can add the +If you'd like to include a field's "human" name, or the optional +parameter some rules allow for (such as max_length), you can add the **{field}** and **{param}** tags to your message, respectively:: $this->form_validation->set_message('min_length', '{field} must have at least {param} characters.'); @@ -559,7 +579,7 @@ Showing Errors Individually =========================== If you prefer to show an error message next to each form field, rather -than as a list, you can use the ``form_error()`` function. +than as a list, you can use the :func:`form_error()` function. Try it! Change your form so that it looks like this:: @@ -860,14 +880,15 @@ use: ========================= ========== ============================================================================================= ======================= Rule Parameter Description Example ========================= ========== ============================================================================================= ======================= -**required** No Returns FALSE if the form element is empty. -**matches** Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item] -**differs** Yes Returns FALSE if the form element does not differ from the one in the parameter. differs[form_item] -**is_unique** Yes Returns FALSE if the form element is not unique to the table and field name in the is_unique[table.field] - parameter. Note: This rule requires :doc:`Query Builder <../database/query_builder>` to be +**required** No Returns FALSE if the form element is empty. +**matches** Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item] +**differs** Yes Returns FALSE if the form element does not differ from the one in the parameter. differs[form_item] +**is_unique** Yes Returns FALSE if the form element is not unique to the table and field name in the is_unique[table.field] + parameter. Note: This rule requires :doc:`Query Builder <../database/query_builder>` to be enabled in order to work. -**max_length** Yes Returns FALSE if the form element is longer then the parameter value. max_length[12] -**exact_length** Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8] +**min_length** Yes Returns FALSE if the form element is shorter than the parameter value. min_length[3] +**max_length** Yes Returns FALSE if the form element is longer than the parameter value. max_length[12] +**exact_length** Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8] **greater_than** Yes Returns FALSE if the form element is less than or equal to the parameter value or not greater_than[8] numeric. **greater_than_equal_to** Yes Returns FALSE if the form element is less than the parameter value, greater_than_equal_to[8] @@ -876,15 +897,15 @@ Rule Parameter Description not numeric. **less_than_equal_to** Yes Returns FALSE if the form element is greater than the parameter value, less_than_equal_to[8] or not numeric. -**alpha** No Returns FALSE if the form element contains anything other than alphabetical characters. +**alpha** No Returns FALSE if the form element contains anything other than alphabetical characters. **alpha_numeric** No Returns FALSE if the form element contains anything other than alpha-numeric characters. **alpha_numeric_spaces** No Returns FALSE if the form element contains anything other than alpha-numeric characters - or spaces. Should be used after trim to avoid spaces at the beginning or end. -**alpha_dash** No Returns FALSE if the form element contains anything other than alpha-numeric characters, - underscores or dashes. -**numeric** No Returns FALSE if the form element contains anything other than numeric characters. -**integer** No Returns FALSE if the form element contains anything other than an integer. -**decimal** No Returns FALSE if the form element contains anything other than a decimal number. + or spaces. Should be used after trim to avoid spaces at the beginning or end. +**alpha_dash** No Returns FALSE if the form element contains anything other than alpha-numeric characters, + underscores or dashes. +**numeric** No Returns FALSE if the form element contains anything other than numeric characters. +**integer** No Returns FALSE if the form element contains anything other than an integer. +**decimal** No Returns FALSE if the form element contains anything other than a decimal number. **is_natural** No Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. **is_natural_no_zero** No Returns FALSE if the form element contains anything other than a natural @@ -933,154 +954,118 @@ Name Parameter Description Class Reference *************** -.. php:class:: Form_validation +.. class:: CI_Form_validation -The following methods are intended for use in your controller. + .. method:: set_rules($field[, $label = ''[, $rules = '']]) -$this->form_validation->set_rules() -=================================== - - .. php:method:: set_rules ($field, $label = '', $rules = '') - - :param string $field: The field name - :param string $label: The field label - :param mixed $rules: The rules, as a string with rules separated by a pipe "|", or an array or rules. - :rtype: Object + :param string $field: Field name + :param string $label: Field label + :param mixed $rules: Validation rules, as a string list separated by a pipe "|", or as an array or rules + :returns: CI_Form_validation instance (method chaining) + :rtype: CI_Form_validation Permits you to set validation rules, as described in the tutorial sections above: - - :ref:`setting-validation-rules` - - :ref:`saving-groups` + - :ref:`setting-validation-rules` + - :ref:`saving-groups` -$this->form_validation->run() -============================= - - .. php:method:: run ($group = '') + .. method:: run([$group = '']) - :param string $group: The name of the validation group to run - :rtype: Boolean + :param string $group: The name of the validation group to run + :returns: TRUE on success, FALSE if validation failed + :rtype: bool 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 method, as described in: :ref:`saving-groups` -$this->form_validation->set_message() -===================================== - - .. php:method:: set_message ($lang, $val = '') + .. method:: set_message($lang[, $val = '']) - :param string $lang: The rule the message is for - :param string $val: The message - :rtype: Object + :param string $lang: The rule the message is for + :param string $val: The message + :returns: CI_Form_validation instance (method chaining) + :rtype: CI_Form_validation Permits you to set custom error messages. See :ref:`setting-error-messages` -$this->form_validation->set_data() -================================== - - .. php:method:: set_data ($data = '') + .. method:: set_error_delimiters([$prefix = '<p>'[, $suffix = '</p>']]) + + :param string $prefix: Error message prefix + :param string $suffix: Error message suffix + :returns: CI_Form_validation instance (method chaining) + :rtype: CI_Form_validation + + Sets the default prefix and suffix for error messages. + + .. method:: set_data($data) - :param array $data: The data to validate + :param array $data: Array of data validate + :returns: CI_Form_validation instance (method chaining) + :rtype: CI_Form_validation Permits you to set an array for validation, instead of using the default - $_POST array. + ``$_POST`` array. -$this->form_validation->reset_validation() -========================================== + .. method:: reset_validation() - .. php:method:: reset_validation () + :returns: CI_Form_validation instance (method chaining) + :rtype: CI_Form_validation Permits you to reset the validation when you validate more than one array. This method should be called before validating each new array. -$this->form_validation->error_array() -===================================== - - .. php:method:: error_array () + .. method:: error_array() - :rtype: Array + :returns: Array of error messages + :rtype: array Returns the error messages as an array. -.. _helper-functions: + .. method:: error_string([$prefix = ''[, $suffix = '']]) -**************** -Helper Reference -**************** + :param string $prefix: Error message prefix + :param string $suffix: Error message suffix + :returns: Error messages as a string + :rtype: string -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. + Returns all error messages (as returned from error_array()) formatted as a + string and separated by a newline character. -form_error() -============ + .. method:: error($field[, $prefix = ''[, $suffix = '']]) -Shows an individual error message associated with the field name -supplied to the function. Example:: + :param string $field: Field name + :param string $prefix: Optional prefix + :param string $suffix: Optional suffix + :returns: Error message string + :rtype: string - <?php echo form_error('username'); ?> + Returns the error message for a specific field, optionally adding a + prefix and/or suffix to it (usually HTML tags). -The error delimiters can be optionally specified. See the -:ref:`changing-delimiters` section above. + .. method:: has_rule($field) -validation_errors() -=================== + :param string $field: Field name + :returns: TRUE if the field has rules set, FALSE if not + :rtype: bool -Shows all error messages as a string: Example:: + Checks to see if there is a rule set for the specified field. - <?php echo validation_errors(); ?> - -The error delimiters can be optionally specified. See the -:ref:`changing-delimiters` section above. - -set_value() -=========== - -Permits you to set the value of an input form or textarea. You must -supply the field name via the first parameter of the function. The -second (optional) parameter allows you to set a default value for the -form. Example:: - - <input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" /> - -The above form will show "0" when loaded for the first time. - -set_select() -============ - -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). - -Example:: - - <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> - </select> - -set_checkbox() -============== - -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'); ?> /> +.. _helper-functions: -set_radio() -=========== +**************** +Helper Reference +**************** -Permits you to display radio buttons in the state they were submitted. -This function is identical to the **set_checkbox()** function above. +Please refer to the :doc:`Form Helper <../helpers/form_helper>` manual for +the following functions: -:: +- :func:`form_error()` +- :func:`validation_errors()` +- :func:`set_value()` +- :func:`set_select()` +- :func:`set_checkbox()` +- :func:`set_radio()` - <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'); ?> /> +Note that these are procedural functions, so they **do not** require you +to prepend them with ``$this->form_validation``.
\ No newline at end of file |