Source for file Rules.php
Documentation is available at Rules.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Forms
22: require_once dirname(__FILE__) .
'/../Object.php';
27: * List of validation & condition rules.
29: * @author David Grudl
30: * @copyright Copyright (c) 2004, 2009 David Grudl
31: * @package Nette\Forms
35: /** @ignore internal */
36: const VALIDATE_PREFIX =
'validate';
39: public static $defaultMessages =
array(
42: /** @var array of Rule */
43: private $rules =
array();
49: private $toggles =
array();
51: /** @var IFormControl */
58: $this->control =
$control;
64: * Adds a validation rule for the current control.
65: * @param mixed rule type
66: * @param string message to display for invalid data
67: * @param mixed optional rule arguments
68: * @return Rules provides a fluent interface
70: public function addRule($operation, $message =
NULL, $arg =
NULL)
73: $rule->control =
$this->control;
74: $rule->operation =
$operation;
75: $this->adjustOperation($rule);
78: if ($message ===
NULL &&
isset(self::$defaultMessages[$rule->operation])) {
79: $rule->message =
self::$defaultMessages[$rule->operation];
81: $rule->message =
$message;
84: if ($this->parent ===
NULL) {
85: // notify only direct rules
86: $this->control->notifyRule($rule);
88: $this->rules[] =
$rule;
95: * Adds a validation condition a returns new branch.
96: * @param mixed condition type
97: * @param mixed optional condition arguments
98: * @return Rules new branch
108: * Adds a validation condition on specified control a returns new branch.
109: * @param IFormControl form control
110: * @param mixed condition type
111: * @param mixed optional condition arguments
112: * @return Rules new branch
117: $rule->control =
$control;
118: $rule->operation =
$operation;
119: $this->adjustOperation($rule);
122: $rule->subRules =
new self($this->control);
123: $rule->subRules->parent =
$this;
125: $this->rules[] =
$rule;
126: return $rule->subRules;
132: * Adds a else statement.
133: * @return Rules else branch
137: $rule =
clone end($this->parent->rules);
138: $rule->isNegative =
!$rule->isNegative;
139: $rule->subRules =
new self($this->parent->control);
140: $rule->subRules->parent =
$this->parent;
141: $this->parent->rules[] =
$rule;
142: return $rule->subRules;
148: * Ends current validation condition.
149: * @return Rules parent branch
153: return $this->parent;
159: * Toggles HTML elememnt visibility.
160: * @param string element id
161: * @param bool hide element?
162: * @return Rules provides a fluent interface
166: $this->toggles[$id] =
$hide;
173: * Validates against ruleset.
174: * @param bool stop before first error?
175: * @return bool is valid?
180: foreach ($this->rules as $rule)
182: if ($rule->control->isDisabled()) continue;
184: $success =
($rule->isNegative xor call_user_func($this->getCallback($rule), $rule->control, $rule->arg));
186: if ($rule->type ===
Rule::CONDITION &&
$success) {
187: $success =
$rule->subRules->validate($onlyCheck);
188: $valid =
$valid &&
$success;
190: } elseif ($rule->type ===
Rule::VALIDATOR &&
!$success) {
194: $rule->control->addError(vsprintf($rule->control->translate($rule->message), (array)
$rule->arg));
196: if ($rule->breakOnFailure) {
207: * Iterates over ruleset.
208: * @return ArrayIterator
212: return new ArrayIterator($this->rules);
222: return $this->toggles;
228: * Process 'operation' string.
232: private function adjustOperation($rule)
235: $rule->isNegative =
TRUE;
236: $rule->operation = ~
$rule->operation;
241: $operation =
is_scalar($rule->operation) ?
" '$rule->operation'" :
'';
242: throw new InvalidArgumentException("Unknown operation$operation for control '{$rule->control->name}'.
");
248: private function getCallback($rule)
250: $op =
$rule->operation;
252: return array($rule->control->getClass(), self::VALIDATE_PREFIX .
ltrim($op, ':'));