Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 5, 2015
1 parent cffefb7 commit 920be81
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/ValueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ protected function removeRule($name, $options)
public function validate($value, $valueIdentifier = null, WrapperInterface $context = null)
{
$this->messages = array();
$isRequired = $this->isRequired();

if (!$isRequired && $value === null) {
if (! $this->isRequired() && $value === null) {
return true;
}

Expand Down Expand Up @@ -130,16 +129,7 @@ protected function runValidations($value, $valueIdentifier, $context)
/* @var $rule \Sirius\Validation\Rule\AbstractValidator */
foreach ($this->rules as $rule) {
$rule->setContext($context);
if (!$rule->validate($value, $valueIdentifier)) {
// if fatal rule fails
if ($rule->getOption('fatal')) {
$exception = new FatalValidationError($rule->getMessage());
$exception->setRule($rule, $value, $valueIdentifier);
throw $exception;
}

$this->addMessage($rule->getMessage());
}
$this->runValidation($rule, $value, $valueIdentifier);

// if field is required and we have an error,
// do not continue with the rest of rules
Expand All @@ -148,4 +138,18 @@ protected function runValidations($value, $valueIdentifier, $context)
}
}
}

protected function runValidation($rule, $value, $valueIdentifier)
{
if (!$rule->validate($value, $valueIdentifier)) {
// if fatal rule fails
if ($rule->getOption('fatal')) {
$exception = new FatalValidationError($rule->getMessage());
$exception->setRule($rule, $value, $valueIdentifier);
throw $exception;
}

$this->addMessage($rule->getMessage());
}
}
}

0 comments on commit 920be81

Please sign in to comment.