From d7cb2490949f30d1563d5d3f0bdd3ab393a6c35a Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 1 Feb 2012 13:53:45 +0100 Subject: [PATCH] [Validator] Deprecated ConstraintValidator methods setMessage(), getMessageTemplate() and getMessageParameters() Had to refactor the validation tests at the same time and fixed various small bugs while doing so. --- ConstraintValidator.php | 16 +++++++++++++--- ConstraintValidatorInterface.php | 16 ++-------------- ConstraintViolation.php | 25 ++++++++++++++++++++++++- ConstraintViolationList.php | 12 +----------- Constraints/BlankValidator.php | 2 +- Constraints/CallbackValidator.php | 7 ++++--- Constraints/ChoiceValidator.php | 8 ++++---- Constraints/CollectionValidator.php | 8 ++++---- Constraints/CountryValidator.php | 2 +- Constraints/DateValidator.php | 6 +++--- Constraints/EmailValidator.php | 2 +- Constraints/FalseValidator.php | 2 +- Constraints/FileValidator.php | 14 +++++++------- Constraints/ImageValidator.php | 10 +++++----- Constraints/IpValidator.php | 2 +- Constraints/LanguageValidator.php | 2 +- Constraints/LocaleValidator.php | 2 +- Constraints/MaxLengthValidator.php | 2 +- Constraints/MaxValidator.php | 4 ++-- Constraints/MinLengthValidator.php | 2 +- Constraints/MinValidator.php | 4 ++-- Constraints/NotBlankValidator.php | 2 +- Constraints/NotNullValidator.php | 2 +- Constraints/NullValidator.php | 2 +- Constraints/RegexValidator.php | 2 +- Constraints/SizeLengthValidator.php | 6 +++--- Constraints/SizeValidator.php | 6 +++--- Constraints/TimeValidator.php | 2 +- Constraints/TrueValidator.php | 2 +- Constraints/TypeValidator.php | 4 ++-- Constraints/UrlValidator.php | 2 +- ExecutionContext.php | 27 +++++++++++++++------------ GraphWalker.php | 13 +------------ 33 files changed, 111 insertions(+), 107 deletions(-) diff --git a/ConstraintValidator.php b/ConstraintValidator.php index 2abf92f9c..e7c045dfa 100644 --- a/ConstraintValidator.php +++ b/ConstraintValidator.php @@ -24,12 +24,18 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface * @var ExecutionContext */ protected $context; + /** * @var string + * + * @deprecated */ private $messageTemplate; + /** * @var array + * + * @deprecated */ private $messageParameters; @@ -46,7 +52,7 @@ public function initialize(ExecutionContext $context) /** * {@inheritDoc} * - * @api + * @deprecated */ public function getMessageTemplate() { @@ -56,7 +62,7 @@ public function getMessageTemplate() /** * {@inheritDoc} * - * @api + * @deprecated */ public function getMessageParameters() { @@ -64,11 +70,15 @@ public function getMessageParameters() } /** - * @api + * Wrapper for $this->context->addViolation() + * + * @deprecated */ protected function setMessage($template, array $parameters = array()) { $this->messageTemplate = $template; $this->messageParameters = $parameters; + + $this->context->addViolation($template, $parameters); } } diff --git a/ConstraintValidatorInterface.php b/ConstraintValidatorInterface.php index 8ef25ada6..a813a6715 100644 --- a/ConstraintValidatorInterface.php +++ b/ConstraintValidatorInterface.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Validator; /** + * @author Bernhard Schussek + * * @api */ interface ConstraintValidatorInterface @@ -34,18 +36,4 @@ function initialize(ExecutionContext $context); * @api */ function isValid($value, Constraint $constraint); - - /** - * @return string - * - * @api - */ - function getMessageTemplate(); - - /** - * @return array - * - * @api - */ - function getMessageParameters(); } diff --git a/ConstraintViolation.php b/ConstraintViolation.php index e528b3aca..a069dabb1 100644 --- a/ConstraintViolation.php +++ b/ConstraintViolation.php @@ -33,6 +33,21 @@ public function __construct($messageTemplate, array $messageParameters, $root, $ $this->invalidValue = $invalidValue; } + /** + * @return string + */ + public function __toString() + { + $class = (string) (is_object($this->root) ? get_class($this->root) : $this->root); + $propertyPath = (string) $this->propertyPath; + + if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) { + $class .= '.'; + } + + return $class . $propertyPath . ":\n " . $this->getMessage(); + } + /** * @return string * @@ -62,7 +77,15 @@ public function getMessageParameters() */ public function getMessage() { - return strtr($this->messageTemplate, $this->messageParameters); + $parameters = $this->messageParameters; + + foreach ($parameters as $i => $parameter) { + if (is_array($parameter)) { + $parameters[$i] = 'Array'; + } + } + + return strtr($this->messageTemplate, $parameters); } public function getRoot() diff --git a/ConstraintViolationList.php b/ConstraintViolationList.php index 96efce4e8..7bda2003e 100644 --- a/ConstraintViolationList.php +++ b/ConstraintViolationList.php @@ -47,17 +47,7 @@ public function __toString() $string = ''; foreach ($this->violations as $violation) { - $root = $violation->getRoot(); - $class = (string) (is_object($root) ? get_class($root) : $root); - $propertyPath = (string) $violation->getPropertyPath(); - if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) { - $class .= '.'; - } - $string .= <<getMessage()} - -EOF; + $string .= $violation . "\n"; } return $string; diff --git a/Constraints/BlankValidator.php b/Constraints/BlankValidator.php index 7c08b15a3..07212eaa6 100644 --- a/Constraints/BlankValidator.php +++ b/Constraints/BlankValidator.php @@ -32,7 +32,7 @@ class BlankValidator extends ConstraintValidator public function isValid($value, Constraint $constraint) { if ('' !== $value && null !== $value) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/CallbackValidator.php b/Constraints/CallbackValidator.php index 34104c0ea..c7a9fa286 100644 --- a/Constraints/CallbackValidator.php +++ b/Constraints/CallbackValidator.php @@ -48,6 +48,7 @@ public function isValid($object, Constraint $constraint) } $methods = $constraint->methods; + $success = true; foreach ($methods as $method) { if (is_array($method) || $method instanceof \Closure) { @@ -55,16 +56,16 @@ public function isValid($object, Constraint $constraint) throw new ConstraintDefinitionException(sprintf('"%s::%s" targeted by Callback constraint is not a valid callable', $method[0], $method[1])); } - call_user_func($method, $object, $this->context); + $success = call_user_func($method, $object, $this->context) && $success; } else { if (!method_exists($object, $method)) { throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist', $method)); } - $object->$method($this->context); + $success = $object->$method($this->context) && $success; } } - return true; + return $success; } } diff --git a/Constraints/ChoiceValidator.php b/Constraints/ChoiceValidator.php index 250fa63eb..c5401eb4f 100644 --- a/Constraints/ChoiceValidator.php +++ b/Constraints/ChoiceValidator.php @@ -66,7 +66,7 @@ public function isValid($value, Constraint $constraint) if ($constraint->multiple) { foreach ($value as $_value) { if (!in_array($_value, $choices, $constraint->strict)) { - $this->setMessage($constraint->multipleMessage, array('{{ value }}' => $_value)); + $this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value)); return false; } @@ -75,18 +75,18 @@ public function isValid($value, Constraint $constraint) $count = count($value); if ($constraint->min !== null && $count < $constraint->min) { - $this->setMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min)); + $this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min)); return false; } if ($constraint->max !== null && $count > $constraint->max) { - $this->setMessage($constraint->maxMessage, array('{{ limit }}' => $constraint->max)); + $this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max)); return false; } } elseif (!in_array($value, $choices, $constraint->strict)) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/CollectionValidator.php b/Constraints/CollectionValidator.php index c380041ee..4697d8576 100644 --- a/Constraints/CollectionValidator.php +++ b/Constraints/CollectionValidator.php @@ -68,8 +68,8 @@ public function isValid($value, Constraint $constraint) $walker->walkConstraint($constr, $value[$field], $group, $propertyPath.'['.$field.']'); } } elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) { - $this->context->addNestedViolationAt('['.$field.']', $constraint->missingFieldsMessage, array( - '{{ field }}' => '"'.$field.'"' + $this->context->addViolationAtRelativePath('['.$field.']', $constraint->missingFieldsMessage, array( + '{{ field }}' => $field ), null); $valid = false; } @@ -78,8 +78,8 @@ public function isValid($value, Constraint $constraint) if (!$constraint->allowExtraFields) { foreach ($value as $field => $fieldValue) { if (!isset($constraint->fields[$field])) { - $this->context->addNestedViolationAt('['.$field.']', $constraint->extraFieldsMessage, array( - '{{ field }}' => '"'.$field.'"' + $this->context->addViolationAtRelativePath('['.$field.']', $constraint->extraFieldsMessage, array( + '{{ field }}' => $field ), $fieldValue); $valid = false; } diff --git a/Constraints/CountryValidator.php b/Constraints/CountryValidator.php index ca681a32d..c6de58050 100644 --- a/Constraints/CountryValidator.php +++ b/Constraints/CountryValidator.php @@ -47,7 +47,7 @@ public function isValid($value, Constraint $constraint) $value = (string) $value; if (!in_array($value, \Symfony\Component\Locale\Locale::getCountries())) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/DateValidator.php b/Constraints/DateValidator.php index 89893bbcd..db4ef7c82 100644 --- a/Constraints/DateValidator.php +++ b/Constraints/DateValidator.php @@ -48,12 +48,12 @@ public function isValid($value, Constraint $constraint) $value = (string) $value; - if (!preg_match(static::PATTERN, $value, $matches)) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) { + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } - return checkdate($matches[2], $matches[3], $matches[1]); + return true; } } diff --git a/Constraints/EmailValidator.php b/Constraints/EmailValidator.php index d4e2adcd4..b26dd5544 100644 --- a/Constraints/EmailValidator.php +++ b/Constraints/EmailValidator.php @@ -58,7 +58,7 @@ public function isValid($value, Constraint $constraint) } if (!$valid) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/FalseValidator.php b/Constraints/FalseValidator.php index bcdaf63db..f473f7a19 100644 --- a/Constraints/FalseValidator.php +++ b/Constraints/FalseValidator.php @@ -39,7 +39,7 @@ public function isValid($value, Constraint $constraint) return true; } - $this->setMessage($constraint->message); + $this->context->addViolation($constraint->message); return false; } diff --git a/Constraints/FileValidator.php b/Constraints/FileValidator.php index aeccf7683..39d93ed49 100644 --- a/Constraints/FileValidator.php +++ b/Constraints/FileValidator.php @@ -44,15 +44,15 @@ public function isValid($value, Constraint $constraint) case UPLOAD_ERR_INI_SIZE: $maxSize = UploadedFile::getMaxFilesize(); $maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize; - $this->setMessage($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' bytes')); + $this->context->addViolation($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' bytes')); return false; case UPLOAD_ERR_FORM_SIZE: - $this->setMessage($constraint->uploadFormSizeErrorMessage); + $this->context->addViolation($constraint->uploadFormSizeErrorMessage); return false; default: - $this->setMessage($constraint->uploadErrorMessage); + $this->context->addViolation($constraint->uploadErrorMessage); return false; } @@ -65,13 +65,13 @@ public function isValid($value, Constraint $constraint) $path = $value instanceof FileObject ? $value->getPathname() : (string) $value; if (!is_file($path)) { - $this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path)); + $this->context->addViolation($constraint->notFoundMessage, array('{{ file }}' => $path)); return false; } if (!is_readable($path)) { - $this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path)); + $this->context->addViolation($constraint->notReadableMessage, array('{{ file }}' => $path)); return false; } @@ -94,7 +94,7 @@ public function isValid($value, Constraint $constraint) } if ($size > $limit) { - $this->setMessage($constraint->maxSizeMessage, array( + $this->context->addViolation($constraint->maxSizeMessage, array( '{{ size }}' => $size.$suffix, '{{ limit }}' => $limit.$suffix, '{{ file }}' => $path, @@ -128,7 +128,7 @@ public function isValid($value, Constraint $constraint) } if (false === $valid) { - $this->setMessage($constraint->mimeTypesMessage, array( + $this->context->addViolation($constraint->mimeTypesMessage, array( '{{ type }}' => '"'.$mime.'"', '{{ types }}' => '"'.implode('", "', $mimeTypes) .'"', '{{ file }}' => $path, diff --git a/Constraints/ImageValidator.php b/Constraints/ImageValidator.php index d9cbc678a..f91113c0f 100644 --- a/Constraints/ImageValidator.php +++ b/Constraints/ImageValidator.php @@ -40,7 +40,7 @@ public function isValid($value, Constraint $constraint) $size = @getimagesize($value); if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) { - $this->setMessage($constraint->sizeNotDetectedMessage); + $this->context->addViolation($constraint->sizeNotDetectedMessage); return false; } @@ -54,7 +54,7 @@ public function isValid($value, Constraint $constraint) } if ($width < $constraint->minWidth) { - $this->setMessage($constraint->minWidthMessage, array( + $this->context->addViolation($constraint->minWidthMessage, array( '{{ width }}' => $width, '{{ min_width }}' => $constraint->minWidth )); @@ -69,7 +69,7 @@ public function isValid($value, Constraint $constraint) } if ($width > $constraint->maxWidth) { - $this->setMessage($constraint->maxWidthMessage, array( + $this->context->addViolation($constraint->maxWidthMessage, array( '{{ width }}' => $width, '{{ max_width }}' => $constraint->maxWidth )); @@ -84,7 +84,7 @@ public function isValid($value, Constraint $constraint) } if ($height < $constraint->minHeight) { - $this->setMessage($constraint->minHeightMessage, array( + $this->context->addViolation($constraint->minHeightMessage, array( '{{ height }}' => $height, '{{ min_height }}' => $constraint->minHeight )); @@ -99,7 +99,7 @@ public function isValid($value, Constraint $constraint) } if ($height > $constraint->maxHeight) { - $this->setMessage($constraint->maxHeightMessage, array( + $this->context->addViolation($constraint->maxHeightMessage, array( '{{ height }}' => $height, '{{ max_height }}' => $constraint->maxHeight )); diff --git a/Constraints/IpValidator.php b/Constraints/IpValidator.php index 724e5fe5c..4bb3fe547 100644 --- a/Constraints/IpValidator.php +++ b/Constraints/IpValidator.php @@ -98,7 +98,7 @@ public function isValid($value, Constraint $constraint) } if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/LanguageValidator.php b/Constraints/LanguageValidator.php index caa5953c8..e5ee7ff6c 100644 --- a/Constraints/LanguageValidator.php +++ b/Constraints/LanguageValidator.php @@ -47,7 +47,7 @@ public function isValid($value, Constraint $constraint) $value = (string) $value; if (!in_array($value, \Symfony\Component\Locale\Locale::getLanguages())) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/LocaleValidator.php b/Constraints/LocaleValidator.php index c51b80806..4ad75447c 100644 --- a/Constraints/LocaleValidator.php +++ b/Constraints/LocaleValidator.php @@ -47,7 +47,7 @@ public function isValid($value, Constraint $constraint) $value = (string) $value; if (!in_array($value, \Symfony\Component\Locale\Locale::getLocales())) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/MaxLengthValidator.php b/Constraints/MaxLengthValidator.php index c4485642e..48bab5fde 100644 --- a/Constraints/MaxLengthValidator.php +++ b/Constraints/MaxLengthValidator.php @@ -51,7 +51,7 @@ public function isValid($value, Constraint $constraint) } if ($length > $constraint->limit) { - $this->setMessage($constraint->message, array( + $this->context->addViolation($constraint->message, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, )); diff --git a/Constraints/MaxValidator.php b/Constraints/MaxValidator.php index 521f5dab9..018f4d0c4 100644 --- a/Constraints/MaxValidator.php +++ b/Constraints/MaxValidator.php @@ -36,7 +36,7 @@ public function isValid($value, Constraint $constraint) } if (!is_numeric($value)) { - $this->setMessage($constraint->invalidMessage, array( + $this->context->addViolation($constraint->invalidMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, )); @@ -45,7 +45,7 @@ public function isValid($value, Constraint $constraint) } if ($value > $constraint->limit) { - $this->setMessage($constraint->message, array( + $this->context->addViolation($constraint->message, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, )); diff --git a/Constraints/MinLengthValidator.php b/Constraints/MinLengthValidator.php index 38bb8e8fb..b148ec794 100644 --- a/Constraints/MinLengthValidator.php +++ b/Constraints/MinLengthValidator.php @@ -51,7 +51,7 @@ public function isValid($value, Constraint $constraint) } if ($length < $constraint->limit) { - $this->setMessage($constraint->message, array( + $this->context->addViolation($constraint->message, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, )); diff --git a/Constraints/MinValidator.php b/Constraints/MinValidator.php index 1106258cf..24e030edb 100644 --- a/Constraints/MinValidator.php +++ b/Constraints/MinValidator.php @@ -36,7 +36,7 @@ public function isValid($value, Constraint $constraint) } if (!is_numeric($value)) { - $this->setMessage($constraint->invalidMessage, array( + $this->context->addViolation($constraint->invalidMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, )); @@ -45,7 +45,7 @@ public function isValid($value, Constraint $constraint) } if ($value < $constraint->limit) { - $this->setMessage($constraint->message, array( + $this->context->addViolation($constraint->message, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, )); diff --git a/Constraints/NotBlankValidator.php b/Constraints/NotBlankValidator.php index d4706c231..5fe90ce94 100644 --- a/Constraints/NotBlankValidator.php +++ b/Constraints/NotBlankValidator.php @@ -32,7 +32,7 @@ class NotBlankValidator extends ConstraintValidator public function isValid($value, Constraint $constraint) { if (false === $value || (empty($value) && '0' != $value)) { - $this->setMessage($constraint->message); + $this->context->addViolation($constraint->message); return false; } diff --git a/Constraints/NotNullValidator.php b/Constraints/NotNullValidator.php index d781698e7..0c8759cef 100644 --- a/Constraints/NotNullValidator.php +++ b/Constraints/NotNullValidator.php @@ -32,7 +32,7 @@ class NotNullValidator extends ConstraintValidator public function isValid($value, Constraint $constraint) { if (null === $value) { - $this->setMessage($constraint->message); + $this->context->addViolation($constraint->message); return false; } diff --git a/Constraints/NullValidator.php b/Constraints/NullValidator.php index ecd87a803..dc04f9cba 100644 --- a/Constraints/NullValidator.php +++ b/Constraints/NullValidator.php @@ -32,7 +32,7 @@ class NullValidator extends ConstraintValidator public function isValid($value, Constraint $constraint) { if (null !== $value) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/RegexValidator.php b/Constraints/RegexValidator.php index 30cdbded2..ebf517bf8 100644 --- a/Constraints/RegexValidator.php +++ b/Constraints/RegexValidator.php @@ -48,7 +48,7 @@ public function isValid($value, Constraint $constraint) $value = (string) $value; if ($constraint->match xor preg_match($constraint->pattern, $value)) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/SizeLengthValidator.php b/Constraints/SizeLengthValidator.php index 916914c03..86de9b4d9 100644 --- a/Constraints/SizeLengthValidator.php +++ b/Constraints/SizeLengthValidator.php @@ -51,7 +51,7 @@ public function isValid($value, Constraint $constraint) } if ($constraint->min == $constraint->max && $length != $constraint->max) { - $this->setMessage($constraint->exactMessage, array( + $this->context->addViolation($constraint->exactMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, )); @@ -60,7 +60,7 @@ public function isValid($value, Constraint $constraint) } if ($length > $constraint->max) { - $this->setMessage($constraint->maxMessage, array( + $this->context->addViolation($constraint->maxMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, )); @@ -69,7 +69,7 @@ public function isValid($value, Constraint $constraint) } if ($length < $constraint->min) { - $this->setMessage($constraint->minMessage, array( + $this->context->addViolation($constraint->minMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->min, )); diff --git a/Constraints/SizeValidator.php b/Constraints/SizeValidator.php index 51fa5fb96..db8bc65c6 100644 --- a/Constraints/SizeValidator.php +++ b/Constraints/SizeValidator.php @@ -36,7 +36,7 @@ public function isValid($value, Constraint $constraint) } if (!is_numeric($value)) { - $this->setMessage($constraint->invalidMessage, array( + $this->context->addViolation($constraint->invalidMessage, array( '{{ value }}' => $value, )); @@ -44,7 +44,7 @@ public function isValid($value, Constraint $constraint) } if ($value > $constraint->max) { - $this->setMessage($constraint->maxMessage, array( + $this->context->addViolation($constraint->maxMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, )); @@ -53,7 +53,7 @@ public function isValid($value, Constraint $constraint) } if ($value < $constraint->min) { - $this->setMessage($constraint->minMessage, array( + $this->context->addViolation($constraint->minMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->min, )); diff --git a/Constraints/TimeValidator.php b/Constraints/TimeValidator.php index 4b117b5bd..6c4b8d538 100644 --- a/Constraints/TimeValidator.php +++ b/Constraints/TimeValidator.php @@ -49,7 +49,7 @@ public function isValid($value, Constraint $constraint) $value = (string) $value; if (!preg_match(static::PATTERN, $value)) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/Constraints/TrueValidator.php b/Constraints/TrueValidator.php index 04dcbef8f..fe58c72eb 100644 --- a/Constraints/TrueValidator.php +++ b/Constraints/TrueValidator.php @@ -39,7 +39,7 @@ public function isValid($value, Constraint $constraint) return true; } - $this->setMessage($constraint->message); + $this->context->addViolation($constraint->message); return false; } diff --git a/Constraints/TypeValidator.php b/Constraints/TypeValidator.php index 84382dd46..71bbed80b 100644 --- a/Constraints/TypeValidator.php +++ b/Constraints/TypeValidator.php @@ -48,8 +48,8 @@ public function isValid($value, Constraint $constraint) return true; } - $this->setMessage($constraint->message, array( - '{{ value }}' => is_object($value) ? get_class($value) : is_array($value) ? 'Array' : (string) $value, + $this->context->addViolation($constraint->message, array( + '{{ value }}' => is_object($value) ? get_class($value) : (is_array($value) ? 'Array' : (string) $value), '{{ type }}' => $constraint->type, )); diff --git a/Constraints/UrlValidator.php b/Constraints/UrlValidator.php index 30021f945..0514ce661 100644 --- a/Constraints/UrlValidator.php +++ b/Constraints/UrlValidator.php @@ -60,7 +60,7 @@ public function isValid($value, Constraint $constraint) $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols)); if (!preg_match($pattern, $value)) { - $this->setMessage($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); return false; } diff --git a/ExecutionContext.php b/ExecutionContext.php index 1118d9d90..300f3b1c6 100644 --- a/ExecutionContext.php +++ b/ExecutionContext.php @@ -80,7 +80,7 @@ public function addViolation($message, array $params = array(), $invalidValue = * @param array $params The parameters parsed into the error message. * @param mixed $invalidValue The invalid, validated value. */ - public function addViolationAt($propertyPath, $message, array $params = array(), $invalidValue = null) + public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null) { $this->globalContext->addViolation(new ConstraintViolation( $message, @@ -93,27 +93,21 @@ public function addViolationAt($propertyPath, $message, array $params = array(), } /** - * Adds a violation at the child of the current validation graph node with - * the given property path. + * Adds a violation at the validation graph node with the given property + * path relative to the current property path. * - * @param string $childPropertyPath The property path of the child node. + * @param string $relativePath The relative property path for the violation. * @param string $message The error message. * @param array $params The parameters parsed into the error message. * @param mixed $invalidValue The invalid, validated value. */ - public function addNestedViolationAt($childPropertyPath, $message, array $params = array(), $invalidValue = null) + public function addViolationAtRelativePath($relativePath, $message, array $params = array(), $invalidValue = null) { - $propertyPath = $this->propertyPath; - - if ('' !== $propertyPath && '' !== $childPropertyPath && '[' !== $childPropertyPath[0]) { - $propertyPath .= '.'; - } - $this->globalContext->addViolation(new ConstraintViolation( $message, $params, $this->globalContext->getRoot(), - $propertyPath . $childPropertyPath, + $this->getAbsolutePropertyPath($relativePath), // check using func_num_args() to allow passing null values func_num_args() === 4 ? $invalidValue : $this->value )); @@ -139,6 +133,15 @@ public function getPropertyPath() return $this->propertyPath; } + public function getAbsolutePropertyPath($relativePath) + { + if ('' !== $this->propertyPath && '' !== $relativePath && '[' !== $relativePath[0]) { + return $this->propertyPath . '.' . $relativePath; + } + + return $this->propertyPath . $relativePath; + } + public function getCurrentClass() { return $this->class; diff --git a/GraphWalker.php b/GraphWalker.php index da6e5a0ae..dbdcfdb70 100644 --- a/GraphWalker.php +++ b/GraphWalker.php @@ -178,17 +178,6 @@ public function walkConstraint(Constraint $constraint, $value, $group, $property ); $validator->initialize($localContext); - - if (!$validator->isValid($value, $constraint)) { - $messageTemplate = $validator->getMessageTemplate(); - $messageParams = $validator->getMessageParameters(); - - // Somewhat ugly hack: Don't add a violation if no message is set. - // This is required if the validator added its violations directly - // to the globalContext already - if (!empty($messageTemplate)) { - $localContext->addViolation($messageTemplate, $messageParams); - } - } + $validator->isValid($value, $constraint); } }