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 https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
17: * @package Nette\Forms
23: require_once dirname(__FILE__) .
'/../Object.php';
28: * List of validation & condition rules.
30: * @author David Grudl
31: * @copyright Copyright (c) 2004, 2009 David Grudl
32: * @package Nette\Forms
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;
99: throw new DeprecatedException('Method addRuleFor() is deprecated. Use addConditionOn() & addRule() construction.');
105: * Adds a validation condition a returns new branch.
106: * @param mixed condition type
107: * @param mixed optional condition arguments
108: * @return Rules new branch
118: * Adds a validation condition on specified control a returns new branch.
119: * @param IFormControl form control
120: * @param mixed condition type
121: * @param mixed optional condition arguments
122: * @return Rules new branch
127: $rule->control =
$control;
128: $rule->operation =
$operation;
129: $this->adjustOperation($rule);
132: $rule->subRules =
new self($this->control);
133: $rule->subRules->parent =
$this;
135: $this->rules[] =
$rule;
136: return $rule->subRules;
142: * Adds a else statement.
143: * @return Rules else branch
147: $rule =
clone end($this->parent->rules);
148: $rule->isNegative =
!$rule->isNegative;
149: $rule->subRules =
new self($this->parent->control);
150: $rule->subRules->parent =
$this->parent;
151: $this->parent->rules[] =
$rule;
152: return $rule->subRules;
158: * Ends current validation condition.
159: * @return Rules parent branch
163: return $this->parent;
169: * Toggles HTML elememnt visibility.
170: * @param string element id
171: * @param bool hide element?
172: * @return Rules provides a fluent interface
176: $this->toggles[$id] =
$hide;
183: * Validates against ruleset.
184: * @param bool stop before first error?
185: * @return bool is valid?
190: foreach ($this->rules as $rule)
192: if ($rule->control->isDisabled()) continue;
194: $success =
($rule->isNegative xor call_user_func($this->getCallback($rule), $rule->control, $rule->arg));
196: if ($rule->type ===
Rule::CONDITION &&
$success) {
197: $success =
$rule->subRules->validate($onlyCheck);
198: $valid =
$valid &&
$success;
200: } elseif ($rule->type ===
Rule::VALIDATOR &&
!$success) {
204: $rule->control->addError(vsprintf($rule->control->translate($rule->message), (array)
$rule->arg));
206: if ($rule->breakOnFailure) {
217: * Iterates over ruleset.
218: * @return ArrayIterator
222: return new ArrayIterator($this->rules);
232: return $this->toggles;
238: * Process 'operation' string.
242: private function adjustOperation($rule)
245: $rule->isNegative =
TRUE;
246: $rule->operation = ~
$rule->operation;
251: $operation =
is_scalar($rule->operation) ?
" '$rule->operation'" :
'';
252: throw new InvalidArgumentException("Unknown operation$operation for control '{$rule->control->name}'.
");
258: private function getCallback($rule)
260: $op =
$rule->operation;
262: return array($rule->control->getClass(), self::VALIDATE_PREFIX .
ltrim($op, ':'));