[Validator] removed the convention that error parameters are delimited with %%
This commit is contained in:
parent
7ead257798
commit
3b1e83380b
@ -31,14 +31,7 @@ class ConstraintViolation
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
$sources = array();
|
||||
$targets = array();
|
||||
foreach ($this->messageParameters as $key => $value) {
|
||||
$sources[] = '{{ '.$key.' }}';
|
||||
$targets[] = (string) $value;
|
||||
}
|
||||
|
||||
return str_replace($sources, $targets, $this->messageTemplate);
|
||||
return str_replace(array_keys($sources), array_values($targets), $this->messageTemplate);
|
||||
}
|
||||
|
||||
public function getRoot()
|
||||
|
@ -23,8 +23,8 @@ class AssertTypeValidator extends ConstraintValidator
|
||||
}
|
||||
|
||||
$this->setMessage($constraint->message, array(
|
||||
'value' => $value,
|
||||
'type' => $constraint->type,
|
||||
'{{ value }}' => $value,
|
||||
'{{ type }}' => $constraint->type,
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -10,7 +10,7 @@ class BlankValidator extends ConstraintValidator
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value !== '' && $value !== null) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class ChoiceValidator extends ConstraintValidator
|
||||
if ($constraint->multiple) {
|
||||
foreach ($value as $_value) {
|
||||
if (!in_array($_value, $choices, true)) {
|
||||
$this->setMessage($constraint->message, array('value' => $_value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $_value));
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -62,18 +62,18 @@ class ChoiceValidator extends ConstraintValidator
|
||||
$count = count($value);
|
||||
|
||||
if ($constraint->min !== null && $count < $constraint->min) {
|
||||
$this->setMessage($constraint->minMessage, array('limit' => $constraint->min));
|
||||
$this->setMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($constraint->max !== null && $count > $constraint->max) {
|
||||
$this->setMessage($constraint->maxMessage, array('limit' => $constraint->max));
|
||||
$this->setMessage($constraint->maxMessage, array('{{ limit }}' => $constraint->max));
|
||||
|
||||
return false;
|
||||
}
|
||||
} elseif (!in_array($value, $choices, true)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ class CollectionValidator extends ConstraintValidator
|
||||
|
||||
if (count($extraFields) > 0 && !$constraint->allowExtraFields) {
|
||||
$this->setMessage($constraint->extraFieldsMessage, array(
|
||||
'fields' => '"'.implode('", "', array_keys($extraFields)).'"'
|
||||
'{{ fields }}' => '"'.implode('", "', array_keys($extraFields)).'"'
|
||||
));
|
||||
|
||||
return false;
|
||||
@ -56,7 +56,7 @@ class CollectionValidator extends ConstraintValidator
|
||||
|
||||
if (count($missingFields) > 0 && !$constraint->allowMissingFields) {
|
||||
$this->setMessage($constraint->missingFieldsMessage, array(
|
||||
'fields' => '"'.implode('", "', $missingFields).'"'
|
||||
'{{ fields }}' => '"'.implode('", "', $missingFields).'"'
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -27,7 +27,7 @@ class DateTimeValidator extends ConstraintValidator
|
||||
$value = (string)$value;
|
||||
|
||||
if (!preg_match(self::PATTERN, $value, $matches)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class DateValidator extends ConstraintValidator
|
||||
$value = (string)$value;
|
||||
|
||||
if (!preg_match(self::PATTERN, $value, $matches)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class EmailValidator extends ConstraintValidator
|
||||
$value = (string)$value;
|
||||
|
||||
if (!preg_match(self::PATTERN, $value)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -32,7 +32,7 @@ class EmailValidator extends ConstraintValidator
|
||||
$host = substr($value, strpos($value, '@') + 1);
|
||||
|
||||
if (!$this->checkMX($host)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -27,13 +27,13 @@ class FileValidator extends ConstraintValidator
|
||||
$path = $value instanceof FileObject ? $value->getPath() : (string)$value;
|
||||
|
||||
if (!file_exists($path)) {
|
||||
$this->setMessage($constraint->notFoundMessage, array('file' => $path));
|
||||
$this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_readable($path)) {
|
||||
$this->setMessage($constraint->notReadableMessage, array('file' => $path));
|
||||
$this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path));
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -57,9 +57,9 @@ class FileValidator extends ConstraintValidator
|
||||
|
||||
if ($size > $limit) {
|
||||
$this->setMessage($constraint->maxSizeMessage, array(
|
||||
'size' => $size . $suffix,
|
||||
'limit' => $limit . $suffix,
|
||||
'file' => $path,
|
||||
'{{ size }}' => $size . $suffix,
|
||||
'{{ limit }}' => $limit . $suffix,
|
||||
'{{ file }}' => $path,
|
||||
));
|
||||
|
||||
return false;
|
||||
@ -73,9 +73,9 @@ class FileValidator extends ConstraintValidator
|
||||
|
||||
if (!in_array($value->getMimeType(), (array)$constraint->mimeTypes)) {
|
||||
$this->setMessage($constraint->mimeTypesMessage, array(
|
||||
'type' => '"'.$value->getMimeType().'"',
|
||||
'types' => '"'.implode('", "', (array)$constraint->mimeTypes).'"',
|
||||
'file' => $path,
|
||||
'{{ type }}' => '"'.$value->getMimeType().'"',
|
||||
'{{ types }}' => '"'.implode('", "', (array)$constraint->mimeTypes).'"',
|
||||
'{{ file }}' => $path,
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -24,8 +24,8 @@ class MaxLengthValidator extends ConstraintValidator
|
||||
|
||||
if ($length > $constraint->limit) {
|
||||
$this->setMessage($constraint->message, array(
|
||||
'value' => $value,
|
||||
'limit' => $constraint->limit,
|
||||
'{{ value }}' => $value,
|
||||
'{{ limit }}' => $constraint->limit,
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -20,8 +20,8 @@ class MaxValidator extends ConstraintValidator
|
||||
|
||||
if ($value > $constraint->limit) {
|
||||
$this->setMessage($constraint->message, array(
|
||||
'value' => $value,
|
||||
'limit' => $constraint->limit,
|
||||
'{{ value }}' => $value,
|
||||
'{{ limit }}' => $constraint->limit,
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -24,8 +24,8 @@ class MinLengthValidator extends ConstraintValidator
|
||||
|
||||
if ($length < $constraint->limit) {
|
||||
$this->setMessage($constraint->message, array(
|
||||
'value' => $value,
|
||||
'limit' => $constraint->limit,
|
||||
'{{ value }}' => $value,
|
||||
'{{ limit }}' => $constraint->limit,
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -20,8 +20,8 @@ class MinValidator extends ConstraintValidator
|
||||
|
||||
if ($value < $constraint->limit) {
|
||||
$this->setMessage($constraint->message, array(
|
||||
'value' => $value,
|
||||
'limit' => $constraint->limit,
|
||||
'{{ value }}' => $value,
|
||||
'{{ limit }}' => $constraint->limit,
|
||||
));
|
||||
|
||||
return false;
|
||||
|
@ -10,7 +10,7 @@ class NullValidator extends ConstraintValidator
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if (!is_null($value)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class RegexValidator extends ConstraintValidator
|
||||
(!$constraint->match && preg_match($constraint->pattern, $value))
|
||||
)
|
||||
{
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class TimeValidator extends ConstraintValidator
|
||||
$value = (string)$value;
|
||||
|
||||
if (!preg_match(self::PATTERN, $value)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class UrlValidator extends ConstraintValidator
|
||||
$pattern = sprintf(self::PATTERN, implode('|', $constraint->protocols));
|
||||
|
||||
if (!preg_match($pattern, $value)) {
|
||||
$this->setMessage($constraint->message, array('value' => $value));
|
||||
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ class ValidValidator extends ConstraintValidator
|
||||
} else if (!is_object($value)) {
|
||||
throw new UnexpectedTypeException($value, 'object or array');
|
||||
} else if ($constraint->class && !$value instanceof $constraint->class) {
|
||||
$this->setMessage($constraint->message, array('class' => $constraint->class));
|
||||
$this->setMessage($constraint->message, array('{{ class }}' => $constraint->class));
|
||||
|
||||
return false;
|
||||
} else {
|
||||
|
@ -101,8 +101,8 @@ class AssertTypeValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'type' => 'numeric',
|
||||
'{{ value }}' => 'foobar',
|
||||
'{{ type }}' => 'numeric',
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ class BlankValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
@ -118,7 +118,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('baz', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'baz',
|
||||
'{{ value }}' => 'baz',
|
||||
));
|
||||
}
|
||||
|
||||
@ -133,7 +133,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid(array('foo', 'baz'), $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'baz',
|
||||
'{{ value }}' => 'baz',
|
||||
));
|
||||
}
|
||||
|
||||
@ -149,7 +149,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'limit' => 2,
|
||||
'{{ limit }}' => 2,
|
||||
));
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'limit' => 2,
|
||||
'{{ limit }}' => 2,
|
||||
));
|
||||
}
|
||||
}
|
@ -76,7 +76,7 @@ class DateTimeValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
@ -70,7 +70,7 @@ class DateValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
@ -70,7 +70,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
@ -52,9 +52,9 @@ class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid($this->path, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'limit' => '10 bytes',
|
||||
'size' => '11 bytes',
|
||||
'file' => $this->path,
|
||||
'{{ limit }}' => '10 bytes',
|
||||
'{{ size }}' => '11 bytes',
|
||||
'{{ file }}' => $this->path,
|
||||
));
|
||||
}
|
||||
|
||||
@ -70,9 +70,9 @@ class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid($this->path, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'limit' => '1 kB',
|
||||
'size' => '1.4 kB',
|
||||
'file' => $this->path,
|
||||
'{{ limit }}' => '1 kB',
|
||||
'{{ size }}' => '1.4 kB',
|
||||
'{{ file }}' => $this->path,
|
||||
));
|
||||
}
|
||||
|
||||
@ -88,9 +88,9 @@ class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid($this->path, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'limit' => '1 MB',
|
||||
'size' => '1.4 MB',
|
||||
'file' => $this->path,
|
||||
'{{ limit }}' => '1 MB',
|
||||
'{{ size }}' => '1.4 MB',
|
||||
'{{ file }}' => $this->path,
|
||||
));
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'file' => 'foobar',
|
||||
'{{ file }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
|
||||
@ -153,9 +153,9 @@ class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid($file, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'type' => '"application/pdf"',
|
||||
'types' => '"image/png", "image/jpg"',
|
||||
'file' => $this->path,
|
||||
'{{ type }}' => '"application/pdf"',
|
||||
'{{ types }}' => '"image/png", "image/jpg"',
|
||||
'{{ file }}' => $this->path,
|
||||
));
|
||||
}
|
||||
}
|
@ -78,8 +78,8 @@ class MaxLengthValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('123456', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => '123456',
|
||||
'limit' => 5,
|
||||
'{{ value }}' => '123456',
|
||||
'{{ limit }}' => 5,
|
||||
));
|
||||
}
|
||||
}
|
@ -72,8 +72,8 @@ class MaxValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid(11, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 11,
|
||||
'limit' => 10,
|
||||
'{{ value }}' => 11,
|
||||
'{{ limit }}' => 10,
|
||||
));
|
||||
}
|
||||
}
|
@ -78,8 +78,8 @@ class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('1234', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => '1234',
|
||||
'limit' => 5,
|
||||
'{{ value }}' => '1234',
|
||||
'{{ limit }}' => 5,
|
||||
));
|
||||
}
|
||||
}
|
@ -72,8 +72,8 @@ class MinValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid(9, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 9,
|
||||
'limit' => 10,
|
||||
'{{ value }}' => 9,
|
||||
'{{ limit }}' => 10,
|
||||
));
|
||||
}
|
||||
}
|
@ -46,7 +46,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid(1, $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 1,
|
||||
'{{ value }}' => 1,
|
||||
));
|
||||
}
|
||||
}
|
@ -72,7 +72,7 @@ class RegexValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
@ -71,7 +71,7 @@ class TimeValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
@ -74,7 +74,7 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
||||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
||||
$this->assertEquals($this->validator->getMessageParameters(), array(
|
||||
'value' => 'foobar',
|
||||
'{{ value }}' => 'foobar',
|
||||
));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user