Skip to content

Commit

Permalink
[Validator] removed the convention that error parameters are delimite…
Browse files Browse the repository at this point in the history
…d with %%
  • Loading branch information
fabpot committed Sep 29, 2010
1 parent 7ead257 commit 3b1e833
Show file tree
Hide file tree
Showing 33 changed files with 70 additions and 77 deletions.
9 changes: 1 addition & 8 deletions src/Symfony/Component/Validator/ConstraintViolation.php
Expand Up @@ -31,14 +31,7 @@ public function getMessageParameters()

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()
Expand Down
Expand Up @@ -23,8 +23,8 @@ public function isValid($value, Constraint $constraint)
}

$this->setMessage($constraint->message, array(
'value' => $value,
'type' => $constraint->type,
'{{ value }}' => $value,
'{{ type }}' => $constraint->type,
));

return false;
Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public function isValid($value, Constraint $constraint)
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;
}
Expand All @@ -62,18 +62,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->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;
}
Expand Down
Expand Up @@ -48,15 +48,15 @@ public function isValid($value, Constraint $constraint)

if (count($extraFields) > 0 && !$constraint->allowExtraFields) {
$this->setMessage($constraint->extraFieldsMessage, array(
'fields' => '"'.implode('", "', array_keys($extraFields)).'"'
'{{ fields }}' => '"'.implode('", "', array_keys($extraFields)).'"'
));

return false;
}

if (count($missingFields) > 0 && !$constraint->allowMissingFields) {
$this->setMessage($constraint->missingFieldsMessage, array(
'fields' => '"'.implode('", "', $missingFields).'"'
'{{ fields }}' => '"'.implode('", "', $missingFields).'"'
));

return false;
Expand Down
Expand Up @@ -27,7 +27,7 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand Down
Expand Up @@ -23,7 +23,7 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand Down
Expand Up @@ -23,7 +23,7 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand All @@ -32,7 +32,7 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Expand Up @@ -27,13 +27,13 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand All @@ -57,9 +57,9 @@ public function isValid($value, Constraint $constraint)

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;
Expand All @@ -73,9 +73,9 @@ public function isValid($value, Constraint $constraint)

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;
Expand Down
Expand Up @@ -24,8 +24,8 @@ public function isValid($value, Constraint $constraint)

if ($length > $constraint->limit) {
$this->setMessage($constraint->message, array(
'value' => $value,
'limit' => $constraint->limit,
'{{ value }}' => $value,
'{{ limit }}' => $constraint->limit,
));

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/MaxValidator.php
Expand Up @@ -20,8 +20,8 @@ public function isValid($value, Constraint $constraint)

if ($value > $constraint->limit) {
$this->setMessage($constraint->message, array(
'value' => $value,
'limit' => $constraint->limit,
'{{ value }}' => $value,
'{{ limit }}' => $constraint->limit,
));

return false;
Expand Down
Expand Up @@ -24,8 +24,8 @@ public function isValid($value, Constraint $constraint)

if ($length < $constraint->limit) {
$this->setMessage($constraint->message, array(
'value' => $value,
'limit' => $constraint->limit,
'{{ value }}' => $value,
'{{ limit }}' => $constraint->limit,
));

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/MinValidator.php
Expand Up @@ -20,8 +20,8 @@ public function isValid($value, Constraint $constraint)

if ($value < $constraint->limit) {
$this->setMessage($constraint->message, array(
'value' => $value,
'limit' => $constraint->limit,
'{{ value }}' => $value,
'{{ limit }}' => $constraint->limit,
));

return false;
Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -26,7 +26,7 @@ public function isValid($value, Constraint $constraint)
(!$constraint->match && preg_match($constraint->pattern, $value))
)
{
$this->setMessage($constraint->message, array('value' => $value));
$this->setMessage($constraint->message, array('{{ value }}' => $value));

return false;
}
Expand Down
Expand Up @@ -23,7 +23,7 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function isValid($value, Constraint $constraint)
$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;
}
Expand Down
Expand Up @@ -27,7 +27,7 @@ public function isValid($value, Constraint $constraint)
} 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 {
Expand Down
Expand Up @@ -101,8 +101,8 @@ public function testMessageIsSet()
$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',
));
}

Expand Down
Expand Up @@ -51,7 +51,7 @@ public function testMessageIsSet()
$this->assertFalse($this->validator->isValid('foobar', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'value' => 'foobar',
'{{ value }}' => 'foobar',
));
}
}
Expand Up @@ -118,7 +118,7 @@ public function testInvalidChoice()
$this->assertFalse($this->validator->isValid('baz', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'value' => 'baz',
'{{ value }}' => 'baz',
));
}

Expand All @@ -133,7 +133,7 @@ public function testInvalidChoiceMultiple()
$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',
));
}

Expand All @@ -149,7 +149,7 @@ public function testTooFewChoices()
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'limit' => 2,
'{{ limit }}' => 2,
));
}

Expand All @@ -165,7 +165,7 @@ public function testTooManyChoices()
$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,
));
}
}
Expand Up @@ -76,7 +76,7 @@ public function testMessageIsSet()
$this->assertFalse($this->validator->isValid('foobar', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'value' => 'foobar',
'{{ value }}' => 'foobar',
));
}
}
Expand Up @@ -70,7 +70,7 @@ public function testMessageIsSet()
$this->assertFalse($this->validator->isValid('foobar', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'value' => 'foobar',
'{{ value }}' => 'foobar',
));
}
}
Expand Up @@ -70,7 +70,7 @@ public function testMessageIsSet()
$this->assertFalse($this->validator->isValid('foobar', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'value' => 'foobar',
'{{ value }}' => 'foobar',
));
}
}

0 comments on commit 3b1e833

Please sign in to comment.