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/libraries | |
parent | 15662dd3d6a21a9602034759129a83e584c3a0d2 (diff) |
Add support for callable form validation rules
Diffstat (limited to 'user_guide_src/source/libraries')
-rw-r--r-- | user_guide_src/source/libraries/form_validation.rst | 38 |
1 files changed, 37 insertions, 1 deletions
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 |