Skip to content

Commit

Permalink
[Validator] Deprecated ConstraintValidator methods setMessage(), getM…
Browse files Browse the repository at this point in the history
…essageTemplate() and getMessageParameters()

Had to refactor the validation tests at the same time and fixed various small bugs while doing so.
  • Loading branch information
webmozart committed Feb 1, 2012
1 parent 1f3548f commit d7cb249
Show file tree
Hide file tree
Showing 33 changed files with 111 additions and 107 deletions.
16 changes: 13 additions & 3 deletions ConstraintValidator.php
Expand Up @@ -24,12 +24,18 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
* @var ExecutionContext
*/
protected $context;

/**
* @var string
*
* @deprecated
*/
private $messageTemplate;

/**
* @var array
*
* @deprecated
*/
private $messageParameters;

Expand All @@ -46,7 +52,7 @@ public function initialize(ExecutionContext $context)
/**
* {@inheritDoc}
*
* @api
* @deprecated
*/
public function getMessageTemplate()
{
Expand All @@ -56,19 +62,23 @@ public function getMessageTemplate()
/**
* {@inheritDoc}
*
* @api
* @deprecated
*/
public function getMessageParameters()
{
return $this->messageParameters;
}

/**
* @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);
}
}
16 changes: 2 additions & 14 deletions ConstraintValidatorInterface.php
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Validator;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
interface ConstraintValidatorInterface
Expand All @@ -34,18 +36,4 @@ function initialize(ExecutionContext $context);
* @api
*/
function isValid($value, Constraint $constraint);

/**
* @return string
*
* @api
*/
function getMessageTemplate();

/**
* @return array
*
* @api
*/
function getMessageParameters();
}
25 changes: 24 additions & 1 deletion ConstraintViolation.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 1 addition & 11 deletions ConstraintViolationList.php
Expand Up @@ -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 .= <<<EOF
{$class}{$propertyPath}:
{$violation->getMessage()}
EOF;
$string .= $violation . "\n";
}

return $string;
Expand Down
2 changes: 1 addition & 1 deletion Constraints/BlankValidator.php
Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions Constraints/CallbackValidator.php
Expand Up @@ -48,23 +48,24 @@ public function isValid($object, Constraint $constraint)
}

$methods = $constraint->methods;
$success = true;

foreach ($methods as $method) {
if (is_array($method) || $method instanceof \Closure) {
if (!is_callable($method)) {
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;
}
}
8 changes: 4 additions & 4 deletions Constraints/ChoiceValidator.php
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions Constraints/CollectionValidator.php
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Constraints/CountryValidator.php
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions Constraints/DateValidator.php
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion Constraints/EmailValidator.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Constraints/FalseValidator.php
Expand Up @@ -39,7 +39,7 @@ public function isValid($value, Constraint $constraint)
return true;
}

$this->setMessage($constraint->message);
$this->context->addViolation($constraint->message);

return false;
}
Expand Down
14 changes: 7 additions & 7 deletions Constraints/FileValidator.php
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions Constraints/ImageValidator.php
Expand Up @@ -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;
}
Expand All @@ -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
));
Expand All @@ -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
));
Expand All @@ -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
));
Expand All @@ -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
));
Expand Down
2 changes: 1 addition & 1 deletion Constraints/IpValidator.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Constraints/LanguageValidator.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Constraints/LocaleValidator.php
Expand Up @@ -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;
}
Expand Down

0 comments on commit d7cb249

Please sign in to comment.