diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index f94abc892e..dcf55d4b92 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -40,21 +40,18 @@ public static function create() public function getErrors() { - return array_merge($this->errors, $this->violated); + return $this->errors; } public function hasError($name) { - return array_key_exists($name, $this->errors) - || array_key_exists($name, $this->violated); + return array_key_exists($name, $this->errors); } public function getError($name) { if (array_key_exists($name, $this->errors)) { return $this->errors[$name]; - } elseif (array_key_exists($name, $this->violated)) { - return $this->violated[$name]; } return null; } @@ -88,7 +85,6 @@ public function getInnerErrors() public function dropAllErrors() { $this->errors = array(); - $this->violated = array(); return $this; } @@ -134,11 +130,9 @@ public function markWrong($name, $label = null) { if (isset($this->primitives[$name])) $this->errors[$name] = self::WRONG; - elseif (isset($this->rules[$name])) - $this->violated[$name] = self::WRONG; else throw new MissingElementException( - $name.' does not match known primitives or rules' + $name.' does not match known primitives' ); if ($label !== null) @@ -154,11 +148,9 @@ public function markGood($primitiveName) { if (isset($this->primitives[$primitiveName])) unset($this->errors[$primitiveName]); - elseif (isset($this->rules[$primitiveName])) - unset($this->violated[$primitiveName]); else throw new MissingElementException( - $primitiveName.' does not match known primitives or rules' + $primitiveName.' does not match known primitives' ); return $this; @@ -200,13 +192,6 @@ public function getTextualErrors() public function getTextualErrorFor($name) { if ( - isset( - $this->violated[$name], - $this->labels[$name][$this->violated[$name]] - ) - ) - return $this->labels[$name][$this->violated[$name]]; - elseif ( isset( $this->errors[$name], $this->labels[$name][$this->errors[$name]] @@ -220,13 +205,6 @@ public function getTextualErrorFor($name) public function getErrorDescriptionFor($name) { if ( - isset( - $this->violated[$name], - $this->describedLabels[$name][$this->violated[$name]] - ) - ) - return $this->describedLabels[$name][$this->violated[$name]]; - elseif ( isset( $this->errors[$name], $this->describedLabels[$name][$this->errors[$name]] @@ -242,14 +220,7 @@ public function getErrorDescriptionFor($name) **/ public function addErrorDescription($name, $errorType, $description) { - - if ( - !isset($this->rules[$name]) - && !$this->get($name)->getName() - ) - throw new MissingElementException( - "knows nothing about '{$name}'" - ); + $this->get($name); $this->describedLabels[$name][$errorType] = $description; @@ -290,6 +261,24 @@ public function getMissingLabel($primitiveName) return $this->getErrorLabel($primitiveName, Form::MISSING); } + /** + * @return Form + **/ + public function checkRules() + { + foreach ($this->getPrimitiveList() as $primitiveName => $primitive) { + if ($primitive instanceof PrimitiveRule) { + if ($primitive->import(null, $this)) { + $this->markGood($primitiveName); + } else { + $this->markWrong($primitiveName); + } + } + } + + return $this; + } + /** * @return Form **/ @@ -328,6 +317,9 @@ public function importOne($primitiveName, $scope) public function importValue($primitiveName, $value) { $prm = $this->get($primitiveName); + if ($prm instanceof PrimitiveRule) { + return $this; + } return $this->checkImportResult($prm, $prm->importValue($value)); } @@ -395,6 +387,10 @@ public function getProto() **/ private function importPrimitive($scope, BasePrimitive $prm) { + if ($prm instanceof PrimitiveRule) { + return $this; + } + if (!$this->importFiltering) { if ($prm instanceof FiltrablePrimitive) { @@ -464,13 +460,7 @@ private function checkImportResult(BasePrimitive $prm, $result) **/ private function addErrorLabel($name, $errorType, $label) { - if ( - !isset($this->rules[$name]) - && !$this->get($name)->getName() - ) - throw new MissingElementException( - "knows nothing about '{$name}'" - ); + $this->get($name); $this->labels[$name][$errorType] = $label; diff --git a/core/Form/Primitive.class.php b/core/Form/Primitive.class.php index 41f6fab878..b5f02203e1 100644 --- a/core/Form/Primitive.class.php +++ b/core/Form/Primitive.class.php @@ -394,5 +394,10 @@ public static function enumList($name) { return new PrimitiveEnumList($name); } + + public static function rule($name) + { + return new PrimitiveRule($name); + } } ?> \ No newline at end of file diff --git a/core/Form/Primitives/PrimitiveRule.class.php b/core/Form/Primitives/PrimitiveRule.class.php new file mode 100644 index 0000000000..1aec6054d8 --- /dev/null +++ b/core/Form/Primitives/PrimitiveRule.class.php @@ -0,0 +1,45 @@ +expression = clone $this->expression; + } + + /** + * @return PrimitiveRule + **/ + public function setExpression(LogicalObject $exp) + { + $this->expression = $exp; + + return $this; + } + + public function import($scope, Form $form = null) + { + Assert::isNotNull($form, 'expects Form as 2-nd argument'); + Assert::isNotNull($this->expression, 'setExpression first'); + + return $this->expression->toBoolean($form) === true; + } + } +?> \ No newline at end of file diff --git a/core/Form/RegulatedForm.class.php b/core/Form/RegulatedForm.class.php index dcf3ac9052..72e64a7b81 100644 --- a/core/Form/RegulatedForm.class.php +++ b/core/Form/RegulatedForm.class.php @@ -17,9 +17,6 @@ **/ abstract class RegulatedForm extends PlainForm { - protected $rules = array(); // forever - protected $violated = array(); // rules - /** * @throws WrongArgumentException * @return Form @@ -28,9 +25,9 @@ public function addRule($name, LogicalObject $rule) { Assert::isString($name); - $this->rules[$name] = $rule; - - return $this; + return $this->add( + Primitive::rule($name)->setExpression($rule) + ); } /** @@ -39,32 +36,17 @@ public function addRule($name, LogicalObject $rule) **/ public function dropRuleByName($name) { - if (isset($this->rules[$name])) { - unset($this->rules[$name]); - return $this; + $rule = $this->get($name); + if (!$rule instanceof PrimitiveRule) { + throw new MissingElementException("no such PrimitiveRule with '{$name}' name"); } - - throw new MissingElementException( - "no such rule with '{$name}' name" - ); + return $this->drop($name); } public function ruleExists($name) { - return isset($this->rules[$name]); - } - - /** - * @return Form - **/ - public function checkRules() - { - foreach ($this->rules as $name => $logicalObject) { - if (!$logicalObject->toBoolean($this)) - $this->violated[$name] = Form::WRONG; - } - - return $this; + return $this->primitiveExists($name) + && $this->get($name) instanceof PrimitiveRule; } } ?> \ No newline at end of file