From 4b90a3746e929f6bbf2b83c71948665c0c31d1fe Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 10 Mar 2014 10:24:24 +0200 Subject: Add support for callable form validation rules --- .../source/libraries/form_validation.rst | 38 +++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'user_guide_src/source/libraries/form_validation.rst') 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 -- cgit v1.2.3-24-g4f1b