summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/libraries/Form_validation.php20
-rw-r--r--user_guide_src/source/libraries/form_validation.rst31
2 files changed, 46 insertions, 5 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 1d654d9f7..b640f1ec1 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -701,6 +701,12 @@ class CI_Form_validation {
{
$callable = TRUE;
}
+ elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
+ {
+ // We have a "named" callable, so save the name
+ $callable = $rule[0];
+ $rule = $rule[1];
+ }
// Strip the parameter (if exists) from the rule
// Rules can contain a parameter: max_length[5]
@@ -712,7 +718,7 @@ class CI_Form_validation {
}
// Call the function that corresponds to the rule
- if ($callback OR $callable)
+ if ($callback OR $callable !== FALSE)
{
if ($callback)
{
@@ -730,8 +736,14 @@ class CI_Form_validation {
else
{
$result = is_array($rule)
- ? $rule[0]->{$rule[1]}($postdata, $param)
- : $rule($postdata, $param);
+ ? $rule[0]->{$rule[1]}($postdata)
+ : $rule($postdata);
+
+ // Is $callable set to a rule name?
+ if ($callable !== FALSE)
+ {
+ $rule = $callable;
+ }
}
// Re-assign the result to the master data array
@@ -791,7 +803,7 @@ class CI_Form_validation {
// Did the rule test negatively? If so, grab the error.
if ($result === FALSE)
{
- // Callable rules don't have named error messages
+ // Callable rules might not have named error messages
if ( ! is_string($rule))
{
return;
diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst
index 2ae56d29a..2b7780ff2 100644
--- a/user_guide_src/source/libraries/form_validation.rst
+++ b/user_guide_src/source/libraries/form_validation.rst
@@ -505,11 +505,40 @@ function::
'required',
function($value)
{
- // Check $value and return TRUE/FALSE
+ // Check $value
}
)
);
+Of course, since a Callable rule by itself is not a string, it isn't
+a rule name either. That is a problem when you want to set error messages
+for them. In order to get around that problem, you can put such rules as
+the second element of an array, with the first one being the rule name::
+
+ $this->form_validation->set_rules(
+ 'username', 'Username',
+ array(
+ 'required',
+ array('username_callable', array($this->users_model', 'valid_username'))
+ )
+ );
+
+Anonymous function (PHP 5.3+) version::
+
+ $this->form_validation->set_rules(
+ 'username', 'Username',
+ array(
+ 'required',
+ array(
+ 'username_callable',
+ function($str)
+ {
+ // Check validity of $str and return TRUE or FALSE
+ }
+ )
+ )
+ );
+
.. _setting-error-messages:
Setting Error Messages