Skip to content

Commit

Permalink
Merge branch '3.1/bugfix/3658-validation-class-method-as-error-name' of
Browse files Browse the repository at this point in the history
https://github.com/synapsestudios/kohana-core into synapsestudios-3.1/bugfix/3658-validation-class-method-as-error-name
  • Loading branch information
isaiahdw committed Feb 6, 2011
2 parents 8c48332 + 25b9736 commit 1cb9690
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions classes/kohana/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,19 @@ public function check()
}
}

if (is_array($rule) OR ! is_string($rule))
// Default the error name to be the rule (except array and lambda rules)
$error_name = $rule;

if (is_array($rule))
{
// This is an array callback, the method name is the error name
$error_name = $rule[1];
$passed = call_user_func_array($rule, $params);
}
elseif ( ! is_string($rule))
{
// This is either a callback as an array or a lambda
// This is a lambda function, there is no error name (errors must be added manually)
$error_name = FALSE;
$passed = call_user_func_array($rule, $params);
}
elseif (method_exists('Valid', $rule))
Expand Down Expand Up @@ -324,10 +334,10 @@ public function check()
if ( ! in_array($rule, $this->_empty_rules) AND ! Valid::not_empty($value))
continue;

if ($passed === FALSE)
if ($passed === FALSE AND $error_name !== FALSE)
{
// Add the rule to the errors
$this->error($field, $rule, $params);
$this->error($field, $error_name, $params);

// This field has an error, stop executing rules
break;
Expand Down
8 changes: 8 additions & 0 deletions tests/kohana/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ public function provider_check()
FALSE,
array('foo' => '1.foo.is_string'),
),
// Test array rules use method as error name
array(
array('foo' => 'test'),
array('foo' => array(array(array('Valid', 'min_length'), array(':value', 10)))),
array(),
FALSE,
array('foo' => 'foo must be at least 10 characters long'),
),
);
}

Expand Down

0 comments on commit 1cb9690

Please sign in to comment.