Skip to content

Commit

Permalink
Using closure at validation rule. Change comments and userguide
Browse files Browse the repository at this point in the history
  • Loading branch information
loguntsov authored and Woody Gilk committed Jan 7, 2012
1 parent 09b8fc5 commit b972911
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 15 additions & 1 deletion classes/kohana/validation.php
Expand Up @@ -189,8 +189,22 @@ public function labels(array $labels)
* // The "password" field must match the "password_repeat" field
* $validation->rule('password', 'matches', array(':validation', 'password', 'password_repeat'));
*
* // Using closure (anonymous function)
* $validation->rule('index',
* function(Validation $array, $field, $value)
* {
* if ($value > 6 AND $value < 10)
* {
* $array->error($field, 'custom');
* }
* }
* , array(':validation', ':field', ':value')
* );
*
* [!!] Errors must be added manually when using closures!
*
* @param string $field field name
* @param callback $rule valid PHP callback
* @param callback $rule valid PHP callback or closure
* @param array $params extra parameters for the rule
* @return $this
*/
Expand Down
6 changes: 3 additions & 3 deletions guide/kohana/security/validation.md
Expand Up @@ -8,7 +8,7 @@ labels
: A label is a human-readable version of the field name.

rules
: A rule is a callback used to decide whether or not to add an error to a field
: A rule is a callback or closure used to decide whether or not to add an error to a field

[!!] Note that any valid [PHP callback](http://php.net/manual/language.pseudo-types.php#language.types.callback) can be used as a rule.

Expand Down Expand Up @@ -50,7 +50,7 @@ Rule name | Function

## Adding Rules

All validation rules are defined as a field name, a method or function (using the [PHP callback](http://php.net/callback) syntax), and an array of parameters:
All validation rules are defined as a field name, a method, a function (using the [PHP callback](http://php.net/callback) syntax) or [closure](http://php.net/manual/functions.anonymous.php), and an array of parameters:

$object->rule($field, $callback, array($parameter1, $parameter2));

Expand Down Expand Up @@ -88,7 +88,7 @@ The [Validation] class will add an error for a field if any of the rules associa
Rules added to empty fields will run, but returning `FALSE` will not automatically add an error for the field. In order for a rule to affect empty fields, you must add the error manually by calling the [Validation::error] method. In order to do this, you must pass the validation object to the rule.

$object->rule($field, 'the_rule', array(':validation', ':field'));

public function the_rule($validation, $field)
{
if (something went wrong)
Expand Down

0 comments on commit b972911

Please sign in to comment.