diff options
author | Andrey Andreev <narf@devilix.net> | 2014-03-10 09:24:24 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-03-10 09:24:24 +0100 |
commit | 4b90a3746e929f6bbf2b83c71948665c0c31d1fe (patch) | |
tree | 18045ab6ddc966c03bf355309000b06ca2293ac9 /user_guide_src/source | |
parent | 15662dd3d6a21a9602034759129a83e584c3a0d2 (diff) |
Add support for callable form validation rules
Diffstat (limited to 'user_guide_src/source')
-rw-r--r-- | user_guide_src/source/changelog.rst | 1 | ||||
-rw-r--r-- | user_guide_src/source/libraries/form_validation.rst | 38 |
2 files changed, 38 insertions, 1 deletions
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 423755b89..36ddbc92e 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -345,6 +345,7 @@ Release Date: Not Released - :doc:`Language <libraries/language>` line keys must now be prefixed with **form_validation_**. - Added rule **alpha_numeric_spaces**. - Added support for custom error messages per field rule. + - Added support for callable rules when they are passed as an array. - :doc:`Caching Library <libraries/caching>` changes include: diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst index 988d6fa25..e51630905 100644 --- a/user_guide_src/source/libraries/form_validation.rst +++ b/user_guide_src/source/libraries/form_validation.rst @@ -474,6 +474,42 @@ then it will be passed as the second argument of your callback method. boolean TRUE/FALSE it is assumed that the data is your newly processed form data. +Callable: Use anything as a rule +================================ + +If callback rules aren't good enough for you (for example, because they are +limited to your controller), don't get disappointed, there's one more way +to create custom rules: anything that ``is_callable()`` would return TRUE for. + +Consider the following example:: + + $this->form_validation->set_rules( + 'username', 'Username', + array( + 'required', + array($this->users_model, 'valid_username') + ) + ); + +The above code would use the ``valid_username()`` method from your +``Users_model`` object. + +This is just an example of course, and callbacks aren't limited to models. +You can use any object/method that accepts the field value as its' first +parameter. Or if you're running PHP 5.3+, you can also use an anonymous +function: + + $this->form_validation->set_rules( + 'username', 'Username', + array( + 'required', + function($value) + { + // Check $value and return TRUE/FALSE + } + ) + ); + .. _setting-error-messages: Setting Error Messages @@ -491,7 +527,7 @@ 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') + array('rule2' => 'Error Message on rule2 for this field_name') ); Where rule corresponds to the name of a particular rule, and Error |