Skip to content

Commit

Permalink
[Validator] Added error codes to all constraints with multiple error …
Browse files Browse the repository at this point in the history
…causes
  • Loading branch information
webmozart committed Sep 30, 2014
1 parent 2f3bb66 commit 3b50bf2
Show file tree
Hide file tree
Showing 61 changed files with 845 additions and 224 deletions.
Expand Up @@ -18,11 +18,20 @@
*/
class Form extends Constraint
{
const NOT_SYNCHRONIZED_ERROR = 1;
const NO_SUCH_FIELD_ERROR = 2;

/**
* Violation code marking an invalid form.
* @deprecated Deprecated since Symfony 2.6, to be removed in 3.0. Use
* {@self NOT_SYNCHRONIZED_ERROR} instead.
*/
const ERR_INVALID = 1;

protected static $errorNames = array(
self::NOT_SYNCHRONIZED_ERROR => 'NOT_SYNCHRONIZED_ERROR',
self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
);

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -102,7 +102,7 @@ public function validate($form, Constraint $constraint)
$this->buildViolation($config->getOption('invalid_message'))
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
->setInvalidValue($form->getViewData())
->setCode(Form::ERR_INVALID)
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
->setCause($form->getTransformationFailure())
->addViolation();
}
Expand All @@ -113,6 +113,7 @@ public function validate($form, Constraint $constraint)
$this->buildViolation($config->getOption('extra_fields_message'))
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
->setInvalidValue($form->getExtraData())
->setCode(Form::NO_SUCH_FIELD_ERROR)
->addViolation();
}
}
Expand Down
Expand Up @@ -66,7 +66,7 @@ public function validateForm(FormEvent $event)
foreach ($violations as $violation) {
// Allow the "invalid" constraint to be put onto
// non-synchronized forms
$allowNonSynchronized = Form::ERR_INVALID === $violation->getCode();
$allowNonSynchronized = $violation->getConstraint() instanceof Form && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();

$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
}
Expand Down
Expand Up @@ -231,7 +231,7 @@ function () { throw new TransformationFailedException(); }
->setParameter('{{ value }}', 'foo')
->setParameter('{{ foo }}', 'bar')
->setInvalidValue('foo')
->setCode(Form::ERR_INVALID)
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
->setCause($is2Dot4Api ? null : $form->getTransformationFailure())
->assertRaised();
}
Expand Down Expand Up @@ -268,7 +268,7 @@ function () { throw new TransformationFailedException(); }
->setParameter('{{ value }}', 'foo')
->setParameter('{{ foo }}', 'bar')
->setInvalidValue('foo')
->setCode(Form::ERR_INVALID)
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
->setCause($is2Dot4Api ? null : $form->getTransformationFailure())
->assertRaised();
}
Expand Down Expand Up @@ -304,7 +304,7 @@ function () { throw new TransformationFailedException(); }
$this->buildViolation('invalid_message_key')
->setParameter('{{ value }}', 'foo')
->setInvalidValue('foo')
->setCode(Form::ERR_INVALID)
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
->setCause($is2Dot4Api ? null : $form->getTransformationFailure())
->assertRaised();
}
Expand Down Expand Up @@ -558,9 +558,11 @@ public function testViolationIfExtraData()

$this->validator->validate($form, new Form());

$this->assertViolation('Extra!', array(
'{{ extra_fields }}' => 'foo',
), 'property.path', array('foo' => 'bar'));
$this->buildViolation('Extra!')
->setParameter('{{ extra_fields }}', 'foo')
->setInvalidValue(array('foo' => 'bar'))
->setCode(Form::NO_SUCH_FIELD_ERROR)
->assertRaised();
}

public function testNoViolationIfAllowExtraData()
Expand Down
Expand Up @@ -66,7 +66,7 @@ protected function setUp()

private function getConstraintViolation($code = null)
{
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code);
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new Form());
}

private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testMapViolation()

public function testMapViolationAllowsNonSyncIfInvalid()
{
$violation = $this->getConstraintViolation(Form::ERR_INVALID);
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR);
$form = $this->getForm('street');

$this->validator->expects($this->once())
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Validator/Constraint.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Exception\MissingOptionsException;

Expand Down Expand Up @@ -50,12 +51,40 @@ abstract class Constraint
*/
const PROPERTY_CONSTRAINT = 'property';

/**
* Maps error codes to the names of their constants
* @var array
*/
protected static $errorNames = array();

/**
* Domain-specific data attached to a constraint
* @var mixed
*/
public $payload;

/**
* Returns the name of the given error code.
*
* @param int $errorCode The error code
*
* @return string The name of the error code
*
* @throws InvalidArgumentException If the error code does not exist
*/
public static function getErrorName($errorCode)
{
if (!isset(static::$errorNames[$errorCode])) {
throw new InvalidArgumentException(sprintf(
'The error code "%s" does not exist for constraint of type "%s".',
$errorCode,
get_called_class()
));
}

return static::$errorNames[$errorCode];
}

/**
* Initializes the constraint with options.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Validator/Constraints/CardScheme.php
Expand Up @@ -20,9 +20,18 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Tim Nagel <t.nagel@infinite.net.au>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class CardScheme extends Constraint
{
const NOT_NUMERIC_ERROR = 1;
const INVALID_FORMAT_ERROR = 2;

protected static $errorNames = array(
self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR',
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
);

public $message = 'Unsupported card type or invalid card number.';
public $schemes;

Expand Down
Expand Up @@ -117,6 +117,7 @@ public function validate($value, Constraint $constraint)
if (!is_numeric($value)) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(CardScheme::NOT_NUMERIC_ERROR)
->addViolation();

return;
Expand All @@ -135,6 +136,7 @@ public function validate($value, Constraint $constraint)

$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(CardScheme::INVALID_FORMAT_ERROR)
->addViolation();
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Choice.php
Expand Up @@ -23,6 +23,16 @@
*/
class Choice extends Constraint
{
const NO_SUCH_CHOICE_ERROR = 1;
const TOO_FEW_ERROR = 2;
const TOO_MANY_ERROR = 3;

protected static $errorNames = array(
self::NO_SUCH_CHOICE_ERROR => 'NO_SUCH_CHOICE_ERROR',
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
);

public $choices;
public $callback;
public $multiple = false;
Expand Down
Expand Up @@ -65,6 +65,7 @@ public function validate($value, Constraint $constraint)
if (!in_array($_value, $choices, $constraint->strict)) {
$this->buildViolation($constraint->multipleMessage)
->setParameter('{{ value }}', $this->formatValue($_value))
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->setInvalidValue($_value)
->addViolation();

Expand All @@ -78,6 +79,7 @@ public function validate($value, Constraint $constraint)
$this->buildViolation($constraint->minMessage)
->setParameter('{{ limit }}', $constraint->min)
->setPlural((int) $constraint->min)
->setCode(Choice::TOO_FEW_ERROR)
->addViolation();

return;
Expand All @@ -87,13 +89,15 @@ public function validate($value, Constraint $constraint)
$this->buildViolation($constraint->maxMessage)
->setParameter('{{ limit }}', $constraint->max)
->setPlural((int) $constraint->max)
->setCode(Choice::TOO_MANY_ERROR)
->addViolation();

return;
}
} elseif (!in_array($value, $choices, $constraint->strict)) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->addViolation();
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Collection.php
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
Expand All @@ -24,6 +23,14 @@
*/
class Collection extends Composite
{
const MISSING_FIELD_ERROR = 1;
const NO_SUCH_FIELD_ERROR = 2;

protected static $errorNames = array(
self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
);

public $fields = array();
public $allowExtraFields = false;
public $allowMissingFields = false;
Expand Down
Expand Up @@ -73,6 +73,7 @@ public function validate($value, Constraint $constraint)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue(null)
->setCode(Collection::MISSING_FIELD_ERROR)
->addViolation();
}
}
Expand All @@ -84,6 +85,7 @@ public function validate($value, Constraint $constraint)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Count.php
Expand Up @@ -24,6 +24,14 @@
*/
class Count extends Constraint
{
const TOO_FEW_ERROR = 1;
const TOO_MANY_ERROR = 2;

protected static $errorNames = array(
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
);

public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';
Expand Down
Expand Up @@ -41,6 +41,7 @@ public function validate($value, Constraint $constraint)
->setParameter('{{ limit }}', $constraint->max)
->setInvalidValue($value)
->setPlural((int) $constraint->max)
->setCode(Count::TOO_MANY_ERROR)
->addViolation();

return;
Expand All @@ -52,6 +53,7 @@ public function validate($value, Constraint $constraint)
->setParameter('{{ limit }}', $constraint->min)
->setInvalidValue($value)
->setPlural((int) $constraint->min)
->setCode(Count::TOO_FEW_ERROR)
->addViolation();
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Date.php
Expand Up @@ -23,5 +23,13 @@
*/
class Date extends Constraint
{
const INVALID_FORMAT_ERROR = 1;
const INVALID_DATE_ERROR = 2;

protected static $errorNames = array(
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
self::INVALID_DATE_ERROR => 'INVALID_DATE_ERROR',
);

public $message = 'This value is not a valid date.';
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Expand Up @@ -23,5 +23,15 @@
*/
class DateTime extends Constraint
{
const INVALID_FORMAT_ERROR = 1;
const INVALID_DATE_ERROR = 2;
const INVALID_TIME_ERROR = 3;

protected static $errorNames = array(
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
self::INVALID_DATE_ERROR => 'INVALID_DATE_ERROR',
self::INVALID_TIME_ERROR => 'INVALID_TIME_ERROR',
);

public $message = 'This value is not a valid datetime.';
}
Expand Up @@ -45,6 +45,7 @@ public function validate($value, Constraint $constraint)
if (!preg_match(static::PATTERN, $value, $matches)) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_FORMAT_ERROR)
->addViolation();

return;
Expand All @@ -53,12 +54,14 @@ public function validate($value, Constraint $constraint)
if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_DATE_ERROR)
->addViolation();
}

if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_TIME_ERROR)
->addViolation();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateValidator.php
Expand Up @@ -62,6 +62,7 @@ public function validate($value, Constraint $constraint)
if (!preg_match(static::PATTERN, $value, $matches)) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Date::INVALID_FORMAT_ERROR)
->addViolation();

return;
Expand All @@ -70,6 +71,7 @@ public function validate($value, Constraint $constraint)
if (!self::checkDate($matches[1], $matches[2], $matches[3])) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Date::INVALID_DATE_ERROR)
->addViolation();
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Email.php
Expand Up @@ -23,6 +23,16 @@
*/
class Email extends Constraint
{
const INVALID_FORMAT_ERROR = 1;
const MX_CHECK_FAILED_ERROR = 2;
const HOST_CHECK_FAILED_ERROR = 3;

protected static $errorNames = array(
self::INVALID_FORMAT_ERROR => 'STRICT_CHECK_FAILED_ERROR',
self::MX_CHECK_FAILED_ERROR => 'MX_CHECK_FAILED_ERROR',
self::HOST_CHECK_FAILED_ERROR => 'HOST_CHECK_FAILED_ERROR',
);

public $message = 'This value is not a valid email address.';
public $checkMX = false;
public $checkHost = false;
Expand Down

0 comments on commit 3b50bf2

Please sign in to comment.