[Validator] Constraint validators now use the 2.5 API. For incompatible validators, legacy validators were created
This commit is contained in:
parent
7e175ef8f3
commit
8e461af756
@ -41,10 +41,11 @@ class AllValidator extends ConstraintValidator
|
|||||||
|
|
||||||
$context = $this->context;
|
$context = $this->context;
|
||||||
$group = $context->getGroup();
|
$group = $context->getGroup();
|
||||||
|
$validator = $context->getValidator()->inContext($context);
|
||||||
|
|
||||||
foreach ($value as $key => $element) {
|
foreach ($value as $key => $element) {
|
||||||
foreach ($constraint->constraints as $constr) {
|
foreach ($constraint->constraints as $constr) {
|
||||||
$context->validateValue($element, $constr, '['.$key.']', $group);
|
$validator->atPath('['.$key.']')->validate($element, $constr, $group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,25 +63,35 @@ class ChoiceValidator extends ConstraintValidator
|
|||||||
if ($constraint->multiple) {
|
if ($constraint->multiple) {
|
||||||
foreach ($value as $_value) {
|
foreach ($value as $_value) {
|
||||||
if (!in_array($_value, $choices, $constraint->strict)) {
|
if (!in_array($_value, $choices, $constraint->strict)) {
|
||||||
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
|
$this->context->buildViolation($constraint->multipleMessage)
|
||||||
|
->setParameter('{{ value }}', $_value)
|
||||||
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$count = count($value);
|
$count = count($value);
|
||||||
|
|
||||||
if ($constraint->min !== null && $count < $constraint->min) {
|
if ($constraint->min !== null && $count < $constraint->min) {
|
||||||
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
|
$this->context->buildViolation($constraint->minMessage)
|
||||||
|
->setParameter('{{ limit }}', $constraint->min)
|
||||||
|
->setPlural((int) $constraint->min)
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($constraint->max !== null && $count > $constraint->max) {
|
if ($constraint->max !== null && $count > $constraint->max) {
|
||||||
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
|
$this->context->buildViolation($constraint->maxMessage)
|
||||||
|
->setParameter('{{ limit }}', $constraint->max)
|
||||||
|
->setPlural((int) $constraint->max)
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} elseif (!in_array($value, $choices, $constraint->strict)) {
|
} elseif (!in_array($value, $choices, $constraint->strict)) {
|
||||||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
|
$this->context->buildViolation($constraint->message)
|
||||||
|
->setParameter('{{ value }}', $value)
|
||||||
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,7 @@ class CollectionValidator extends ConstraintValidator
|
|||||||
// to validate() instead.
|
// to validate() instead.
|
||||||
$context = $this->context;
|
$context = $this->context;
|
||||||
$group = $context->getGroup();
|
$group = $context->getGroup();
|
||||||
|
$validator = $context->getValidator()->inContext($context);
|
||||||
|
|
||||||
foreach ($constraint->fields as $field => $fieldConstraint) {
|
foreach ($constraint->fields as $field => $fieldConstraint) {
|
||||||
if (
|
if (
|
||||||
@ -58,21 +59,23 @@ class CollectionValidator extends ConstraintValidator
|
|||||||
($value instanceof \ArrayAccess && $value->offsetExists($field))
|
($value instanceof \ArrayAccess && $value->offsetExists($field))
|
||||||
) {
|
) {
|
||||||
foreach ($fieldConstraint->constraints as $constr) {
|
foreach ($fieldConstraint->constraints as $constr) {
|
||||||
$context->validateValue($value[$field], $constr, '['.$field.']', $group);
|
$validator->atPath('['.$field.']')->validate($value[$field], $constr, $group);
|
||||||
}
|
}
|
||||||
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
|
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
|
||||||
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
|
$context->buildViolation($constraint->missingFieldsMessage)
|
||||||
'{{ field }}' => $field
|
->atPath('['.$field.']')
|
||||||
), null);
|
->setParameter('{{ field }}', $field)
|
||||||
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$constraint->allowExtraFields) {
|
if (!$constraint->allowExtraFields) {
|
||||||
foreach ($value as $field => $fieldValue) {
|
foreach ($value as $field => $fieldValue) {
|
||||||
if (!isset($constraint->fields[$field])) {
|
if (!isset($constraint->fields[$field])) {
|
||||||
$context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
|
$context->buildViolation($constraint->extraFieldsMessage)
|
||||||
'{{ field }}' => $field
|
->atPath('['.$field.']')
|
||||||
), $fieldValue);
|
->setParameter('{{ field }}', $field)
|
||||||
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,28 +36,34 @@ class CountValidator extends ConstraintValidator
|
|||||||
$count = count($value);
|
$count = count($value);
|
||||||
|
|
||||||
if ($constraint->min == $constraint->max && $count != $constraint->min) {
|
if ($constraint->min == $constraint->max && $count != $constraint->min) {
|
||||||
$this->context->addViolation($constraint->exactMessage, array(
|
$this->context->buildViolation($constraint->exactMessage)
|
||||||
'{{ count }}' => $count,
|
->setParameter('{{ count }}', $count)
|
||||||
'{{ limit }}' => $constraint->min,
|
->setParameter('{{ limit }}', $constraint->min)
|
||||||
), $value, (int) $constraint->min);
|
->setValue($value)
|
||||||
|
->setPlural((int) $constraint->min)
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $constraint->max && $count > $constraint->max) {
|
if (null !== $constraint->max && $count > $constraint->max) {
|
||||||
$this->context->addViolation($constraint->maxMessage, array(
|
$this->context->buildViolation($constraint->maxMessage)
|
||||||
'{{ count }}' => $count,
|
->setParameter('{{ count }}', $count)
|
||||||
'{{ limit }}' => $constraint->max,
|
->setParameter('{{ limit }}', $constraint->max)
|
||||||
), $value, (int) $constraint->max);
|
->setValue($value)
|
||||||
|
->setPlural((int) $constraint->max)
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $constraint->min && $count < $constraint->min) {
|
if (null !== $constraint->min && $count < $constraint->min) {
|
||||||
$this->context->addViolation($constraint->minMessage, array(
|
$this->context->buildViolation($constraint->minMessage)
|
||||||
'{{ count }}' => $count,
|
->setParameter('{{ count }}', $count)
|
||||||
'{{ limit }}' => $constraint->min,
|
->setParameter('{{ limit }}', $constraint->min)
|
||||||
), $value, (int) $constraint->min);
|
->setValue($value)
|
||||||
|
->setPlural((int) $constraint->min)
|
||||||
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
|
||||||
|
*/
|
||||||
|
class LegacyAllValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (!$constraint instanceof All) {
|
||||||
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($value) && !$value instanceof \Traversable) {
|
||||||
|
throw new UnexpectedTypeException($value, 'array or Traversable');
|
||||||
|
}
|
||||||
|
|
||||||
|
$context = $this->context;
|
||||||
|
$group = $context->getGroup();
|
||||||
|
|
||||||
|
foreach ($value as $key => $element) {
|
||||||
|
foreach ($constraint->constraints as $constr) {
|
||||||
|
$context->validateValue($element, $constr, '['.$key.']', $group);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ChoiceValidator validates that the value is one of the expected values.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Florian Eckerstorfer <florian@eckerstorfer.org>
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
|
||||||
|
*/
|
||||||
|
class LegacyChoiceValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (!$constraint instanceof Choice) {
|
||||||
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$constraint->choices && !$constraint->callback) {
|
||||||
|
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($constraint->multiple && !is_array($value)) {
|
||||||
|
throw new UnexpectedTypeException($value, 'array');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($constraint->callback) {
|
||||||
|
if (is_callable(array($this->context->getClassName(), $constraint->callback))) {
|
||||||
|
$choices = call_user_func(array($this->context->getClassName(), $constraint->callback));
|
||||||
|
} elseif (is_callable($constraint->callback)) {
|
||||||
|
$choices = call_user_func($constraint->callback);
|
||||||
|
} else {
|
||||||
|
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$choices = $constraint->choices;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($constraint->multiple) {
|
||||||
|
foreach ($value as $_value) {
|
||||||
|
if (!in_array($_value, $choices, $constraint->strict)) {
|
||||||
|
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = count($value);
|
||||||
|
|
||||||
|
if ($constraint->min !== null && $count < $constraint->min) {
|
||||||
|
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($constraint->max !== null && $count > $constraint->max) {
|
||||||
|
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} elseif (!in_array($value, $choices, $constraint->strict)) {
|
||||||
|
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
|
||||||
|
*/
|
||||||
|
class LegacyCollectionValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (!$constraint instanceof Collection) {
|
||||||
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
|
||||||
|
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need to keep the initialized context when CollectionValidator
|
||||||
|
// calls itself recursively (Collection constraints can be nested).
|
||||||
|
// Since the context of the validator is overwritten when initialize()
|
||||||
|
// is called for the nested constraint, the outer validator is
|
||||||
|
// acting on the wrong context when the nested validation terminates.
|
||||||
|
//
|
||||||
|
// A better solution - which should be approached in Symfony 3.0 - is to
|
||||||
|
// remove the initialize() method and pass the context as last argument
|
||||||
|
// to validate() instead.
|
||||||
|
$context = $this->context;
|
||||||
|
$group = $context->getGroup();
|
||||||
|
|
||||||
|
foreach ($constraint->fields as $field => $fieldConstraint) {
|
||||||
|
if (
|
||||||
|
// bug fix issue #2779
|
||||||
|
(is_array($value) && array_key_exists($field, $value)) ||
|
||||||
|
($value instanceof \ArrayAccess && $value->offsetExists($field))
|
||||||
|
) {
|
||||||
|
foreach ($fieldConstraint->constraints as $constr) {
|
||||||
|
$context->validateValue($value[$field], $constr, '['.$field.']', $group);
|
||||||
|
}
|
||||||
|
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
|
||||||
|
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
|
||||||
|
'{{ field }}' => $field
|
||||||
|
), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$constraint->allowExtraFields) {
|
||||||
|
foreach ($value as $field => $fieldValue) {
|
||||||
|
if (!isset($constraint->fields[$field])) {
|
||||||
|
$context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
|
||||||
|
'{{ field }}' => $field
|
||||||
|
), $fieldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
|
||||||
|
*/
|
||||||
|
class LegacyCountValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (null === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($value) && !$value instanceof \Countable) {
|
||||||
|
throw new UnexpectedTypeException($value, 'array or \Countable');
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = count($value);
|
||||||
|
|
||||||
|
if ($constraint->min == $constraint->max && $count != $constraint->min) {
|
||||||
|
$this->context->addViolation($constraint->exactMessage, array(
|
||||||
|
'{{ count }}' => $count,
|
||||||
|
'{{ limit }}' => $constraint->min,
|
||||||
|
), $value, (int) $constraint->min);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $constraint->max && $count > $constraint->max) {
|
||||||
|
$this->context->addViolation($constraint->maxMessage, array(
|
||||||
|
'{{ count }}' => $count,
|
||||||
|
'{{ limit }}' => $constraint->max,
|
||||||
|
), $value, (int) $constraint->max);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $constraint->min && $count < $constraint->min) {
|
||||||
|
$this->context->addViolation($constraint->minMessage, array(
|
||||||
|
'{{ count }}' => $count,
|
||||||
|
'{{ limit }}' => $constraint->min,
|
||||||
|
), $value, (int) $constraint->min);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
|
||||||
|
*/
|
||||||
|
class LegacyLengthValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (!$constraint instanceof Length) {
|
||||||
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $value || '' === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
||||||
|
throw new UnexpectedTypeException($value, 'string');
|
||||||
|
}
|
||||||
|
|
||||||
|
$stringValue = (string) $value;
|
||||||
|
|
||||||
|
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
|
||||||
|
$length = grapheme_strlen($stringValue);
|
||||||
|
} elseif (function_exists('mb_strlen')) {
|
||||||
|
$length = mb_strlen($stringValue, $constraint->charset);
|
||||||
|
} else {
|
||||||
|
$length = strlen($stringValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($constraint->min == $constraint->max && $length != $constraint->min) {
|
||||||
|
$this->context->addViolation($constraint->exactMessage, array(
|
||||||
|
'{{ value }}' => $stringValue,
|
||||||
|
'{{ limit }}' => $constraint->min,
|
||||||
|
), $value, (int) $constraint->min);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $constraint->max && $length > $constraint->max) {
|
||||||
|
$this->context->addViolation($constraint->maxMessage, array(
|
||||||
|
'{{ value }}' => $stringValue,
|
||||||
|
'{{ limit }}' => $constraint->max,
|
||||||
|
), $value, (int) $constraint->max);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $constraint->min && $length < $constraint->min) {
|
||||||
|
$this->context->addViolation($constraint->minMessage, array(
|
||||||
|
'{{ value }}' => $stringValue,
|
||||||
|
'{{ limit }}' => $constraint->min,
|
||||||
|
), $value, (int) $constraint->min);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -48,28 +48,34 @@ class LengthValidator extends ConstraintValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($constraint->min == $constraint->max && $length != $constraint->min) {
|
if ($constraint->min == $constraint->max && $length != $constraint->min) {
|
||||||
$this->context->addViolation($constraint->exactMessage, array(
|
$this->context->buildViolation($constraint->exactMessage)
|
||||||
'{{ value }}' => $stringValue,
|
->setParameter('{{ value }}', $stringValue)
|
||||||
'{{ limit }}' => $constraint->min,
|
->setParameter('{{ limit }}', $constraint->min)
|
||||||
), $value, (int) $constraint->min);
|
->setValue($value)
|
||||||
|
->setPlural((int) $constraint->min)
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $constraint->max && $length > $constraint->max) {
|
if (null !== $constraint->max && $length > $constraint->max) {
|
||||||
$this->context->addViolation($constraint->maxMessage, array(
|
$this->context->buildViolation($constraint->maxMessage)
|
||||||
'{{ value }}' => $stringValue,
|
->setParameter('{{ value }}', $stringValue)
|
||||||
'{{ limit }}' => $constraint->max,
|
->setParameter('{{ limit }}', $constraint->max)
|
||||||
), $value, (int) $constraint->max);
|
->setValue($value)
|
||||||
|
->setPlural((int) $constraint->max)
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $constraint->min && $length < $constraint->min) {
|
if (null !== $constraint->min && $length < $constraint->min) {
|
||||||
$this->context->addViolation($constraint->minMessage, array(
|
$this->context->buildViolation($constraint->minMessage)
|
||||||
'{{ value }}' => $stringValue,
|
->setParameter('{{ value }}', $stringValue)
|
||||||
'{{ limit }}' => $constraint->min,
|
->setParameter('{{ limit }}', $constraint->min)
|
||||||
), $value, (int) $constraint->min);
|
->setValue($value)
|
||||||
|
->setPlural((int) $constraint->min)
|
||||||
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Backwards compatible implementation of the {@link ConstraintValidatorFactoryInterface}.
|
||||||
|
*
|
||||||
|
* This class uses legacy constraint validators where possible to ensure
|
||||||
|
* compatibility with the 2.4 validator API.
|
||||||
|
*
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
|
||||||
|
*/
|
||||||
|
class LegacyConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
|
||||||
|
{
|
||||||
|
protected $validators = array();
|
||||||
|
|
||||||
|
private $propertyAccessor;
|
||||||
|
|
||||||
|
public function __construct($propertyAccessor = null)
|
||||||
|
{
|
||||||
|
$this->propertyAccessor = $propertyAccessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getInstance(Constraint $constraint)
|
||||||
|
{
|
||||||
|
switch (get_class($constraint)) {
|
||||||
|
case __NAMESPACE__.'\\Constraints\\All':
|
||||||
|
$className = __NAMESPACE__.'\\Constraints\\LegacyAllValidator';
|
||||||
|
break;
|
||||||
|
case __NAMESPACE__.'\\Constraints\\Choice':
|
||||||
|
$className = __NAMESPACE__.'\\Constraints\\LegacyChoiceValidator';
|
||||||
|
break;
|
||||||
|
case __NAMESPACE__.'\\Constraints\\Collection':
|
||||||
|
$className = __NAMESPACE__.'\\Constraints\\LegacyCollectionValidator';
|
||||||
|
break;
|
||||||
|
case __NAMESPACE__.'\\Constraints\\Count':
|
||||||
|
$className = __NAMESPACE__.'\\Constraints\\LegacyCountValidator';
|
||||||
|
break;
|
||||||
|
case __NAMESPACE__.'\\Constraints\\Length':
|
||||||
|
$className = __NAMESPACE__.'\\Constraints\\LegacyLengthValidator';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$className = $constraint->validatedBy();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($this->validators[$className])) {
|
||||||
|
$this->validators[$className] = 'validator.expression' === $className
|
||||||
|
? new ExpressionValidator($this->propertyAccessor)
|
||||||
|
: new $className();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->validators[$className];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user