/** * @param $value * @param IValidationData $data * * @return ValidationResult */ public function check($value, IValidationData $data = null) { $result = new ValidationResult(); foreach ($this->getConstraints() as $constraint) { if (!$constraint->check($value, $data)) { $result->addError(new ValidationError($this->getName(), $value, $constraint)); } } return $result; }
/** * Indicates wether the object passed as a parameter is valid as defined by the rules. * * Loops through the rules defined for this validator, calling the rule's validate method, * passing $obj as a parameter. Returns false at the first rule that fails. If all rules * succeed, true is returned. * * @param mixed $obj is the object on which we want to test the validations * @return bool indicating wether $obj was valid according to the rules */ public function validate($obj) { $results = new ValidationResult(); foreach ($this->rules as $rule) { //Get the rule condition from the array if there is one $rule_id = $rule->get_id(); //If a condition exists for the rule if (array_key_exists($rule_id, $this->rules_conditions)) { $rule_condition = $this->rules_conditions[$rule_id]; //Skip the rule if the condition is not met if (!$rule_condition($obj)) { continue; } } //For each rule, we get the props associated with their id //in the other array containing the props. foreach ($this->rules_props[$rule_id] as $prop) { //Check if $prop is a method or property if (method_exists($obj, $prop)) { $value = $obj->{$prop}(); } else { if (property_exists($obj, $prop)) { $value = $obj->{$prop}; } } if (!$rule->validate($value)) { if (array_key_exists($rule_id, $this->rules_messages)) { $error_message = $this->rules_messages[$rule_id]; } else { $error_message = 'Field is invalid'; } $results->addError($prop, $error_message); } } } return $results; }
/** * @expectedException PHPUnit_Framework_Error_Notice * @expectedExceptionDescription There are multiple errors, returning only the first */ public function testGetErrorWarningWithMultiple() { $validation = new ValidationResult(); $validation->addError("Foo"); $validation->addError("Bar"); $validation->getError(); }