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 0417282 commit 9153f0e
Show file tree
Hide file tree
Showing 72 changed files with 1,533 additions and 859 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -300,6 +300,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* changed default value for `extraFieldsMessage` and `missingFieldsMessage`
in Collection constraint
* made ExecutionContext immutable
* deprecated Constraint methods `setMessage`, `getMessageTemplate` and
`getMessageParameters`

### Yaml

Expand Down
34 changes: 34 additions & 0 deletions UPGRADE-2.1.md
Expand Up @@ -141,3 +141,37 @@ UPGRADE FROM 2.0 to 2.1
$builder->add('tags', 'collection', array('prototype' => '__proto__'));

// results in the name "__proto__" in the template

* The methods `setMessage`, `getMessageTemplate` and `getMessageParameters`
in Constraint were deprecated

If you have implemented custom validators, you should use either of the
`addViolation*` methods of the context object instead.

Before:

public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->setMessage($constraint->message, array(
'{{ value }}' => $value,
));

return false;
}
}

After:

public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
));

return false;
}
}
Expand Up @@ -102,7 +102,7 @@ public function isValid($entity, Constraint $constraint)
return true;
}

$this->context->addNestedViolationAt($fields[0], $constraint->message, array(), $criteria[$fields[0]]);
$this->context->addViolationAtRelativePath($fields[0], $constraint->message, array(), $criteria[$fields[0]]);

return true; // all true, we added the violation already!
}
Expand Down
Expand Up @@ -40,7 +40,7 @@ public function isValid($password, Constraint $constraint)
$encoder = $this->encoderFactory->getEncoder($user);

if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
$this->setMessage($constraint->message);
$this->context->addViolation($constraint->message);

return false;
}
Expand Down
16 changes: 13 additions & 3 deletions src/Symfony/Component/Validator/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 src/Symfony/Component/Validator/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 src/Symfony/Component/Validator/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 src/Symfony/Component/Validator/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
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
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;
}
}
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
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
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 src/Symfony/Component/Validator/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;
}
}
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
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 src/Symfony/Component/Validator/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

0 comments on commit 9153f0e

Please sign in to comment.