8000 [Validator] Fixed string conversion in constraint violations by webmozart · Pull Request #10687 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Fixed string conversion in constraint violations #10687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 30, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Validator] Added "{{ value }}" parameters where they were missing
  • Loading branch information
webmozart committed Jul 24, 2014
commit 5aa7e6dbe0d27c9cfcfb4cb8d44da1b061c2280d
7 changes: 4 additions & 3 deletions src/Symfony/Component/Validator/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ protected function valueToType($value)
/**
* Returns a string representation of the value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some more explanations about when/why/how this is ever called? Something along the lines of:

"This method is used when a constraint error message contains the {{ value }} placeholder. By default, Symfony never includes it, but the developer can override those error messages. It's up to the developer to make sure it actually makes sense to include such string representation of submitted values in error messages."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*
* @param mixed $value
* @param mixed $value
* @param Boolean $formatDates
*
* @return string
*/
protected function valueToString($value)
protected function valueToString($value, $formatDates = false)
{
if ($value instanceof \DateTime) {
if ($formatDates && $value instanceof \DateTime) {
if (class_exists('IntlDateFormatter')) {
$locale = \Locale::getDefault();
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function validate($value, Constraint $constraint)

if (!$this->compareValues($value, $constraint->value)) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $this->valueToString($value),
'{{ compared_value }}' => $this->valueToString($constraint->value),
'{{ value }}' => $this->valueToString($value, true),
'{{ compared_value }}' => $this->valueToString($constraint->value, true),
'{{ compared_value_type }}' => $this->valueToType($constraint->value)
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function validate($value, Constraint $constraint)
return;
}

$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $this->valueToString($value),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
* @see http://en.wikipedia.org/wiki/Luhn_algorithm
* @author Tim Nagel <t.nagel@infinite.net.au>
* @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class LuhnValidator extends ConstraintValidator
{
/**
* Validates a creditcard number with the Luhn algorithm.
* Validates a credit card number with the Luhn algorithm.
*
* @param mixed $value
* @param Constraint $constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class NotBlankValidator extends ConstraintValidator
public function validate($value, Constraint $constraint)
{
if (false === $value || (empty($value) && '0' != $value)) {
$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $this->valueToString($value),
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ class NullValidator extends ConstraintValidator
public function validate($value, Constraint $constraint)
{
if (null !== $value) {
if (is_object($value)) {
$value = get_class($value);
} elseif (is_array($value)) {
$value = 'Array';
}

$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
'{{ value }}' => $this->valueToString($value),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function validate($value, Constraint $constraint)
}

if (true !== $value && 1 !== $value && '1' !== $value) {
$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $this->valueToString($value),
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testTrueIsInvalid()

$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array());
->with('myMessage', array('{{ value }}' => 'true'));

$this->validator->validate(true, $constraint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testNullIsValid()
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value, $readableValue)
public function testInvalidValues($value, $valueAsString)
{
$constraint = new Null(array(
'message' => 'myMessage'
Expand All @@ -52,7 +52,7 @@ public function testInvalidValues($value, $readableValue)
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $readableValue,
'{{ value }}' => $valueAsString,
));

$this->validator->validate($value, $constraint);
Expand All @@ -61,12 +61,13 @@ public function testInvalidValues($value, $readableValue)
public function getInvalidValues()
{
return array(
array(0, 0),
array(false, false),
array(true, true),
array('', ''),
array('foo bar', 'foo bar'),
array(new \DateTime(), 'DateTime'),
array(0, '0'),
array(false, 'false'),
array(true, 'true'),
array('', '""'),
array('foo bar', '"foo bar"'),
array(new \DateTime(), 'Object(DateTime)'),
array(new \stdClass(), 'Object(stdClass)'),
array(array(), 'Array'),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function testFalseIsInvalid()
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => 'false',
));

$this->validator->validate(false, $constraint);
Expand Down
0