[Form] Fixed invalid value passed to the constraint violation by the MinLength and MaxLength validators

This commit is contained in:
Bernhard Schussek 2012-07-09 18:53:54 +02:00
parent 35e8d086ed
commit f093caa6ac
4 changed files with 18 additions and 18 deletions

View File

@ -40,21 +40,21 @@ class MaxLengthValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string'); throw new UnexpectedTypeException($value, 'string');
} }
$value = (string) $value; $stringValue = (string) $value;
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) { if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($value); $length = grapheme_strlen($stringValue);
} elseif (function_exists('mb_strlen')) { } elseif (function_exists('mb_strlen')) {
$length = mb_strlen($value, $constraint->charset); $length = mb_strlen($stringValue, $constraint->charset);
} else { } else {
$length = strlen($value); $length = strlen($stringValue);
} }
if ($length > $constraint->limit) { if ($length > $constraint->limit) {
$this->context->addViolation($constraint->message, array( $this->context->addViolation($constraint->message, array(
'{{ value }}' => $value, '{{ value }}' => $stringValue,
'{{ limit }}' => $constraint->limit, '{{ limit }}' => $constraint->limit,
), null, (int) $constraint->limit); ), $value, (int) $constraint->limit);
} }
} }
} }

View File

@ -40,21 +40,21 @@ class MinLengthValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string'); throw new UnexpectedTypeException($value, 'string');
} }
$value = (string) $value; $stringValue = (string) $value;
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) { if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($value); $length = grapheme_strlen($stringValue);
} elseif (function_exists('mb_strlen')) { } elseif (function_exists('mb_strlen')) {
$length = mb_strlen($value, $constraint->charset); $length = mb_strlen($stringValue, $constraint->charset);
} else { } else {
$length = strlen($value); $length = strlen($stringValue);
} }
if ($length < $constraint->limit) { if ($length < $constraint->limit) {
$this->context->addViolation($constraint->message, array( $this->context->addViolation($constraint->message, array(
'{{ value }}' => $value, '{{ value }}' => $stringValue,
'{{ limit }}' => $constraint->limit, '{{ limit }}' => $constraint->limit,
), null, (int) $constraint->limit); ), $value, (int) $constraint->limit);
} }
} }
} }

View File

@ -98,10 +98,10 @@ class MaxLengthValidatorTest extends \PHPUnit_Framework_TestCase
$this->context->expects($this->once()) $this->context->expects($this->once())
->method('addViolation') ->method('addViolation')
->with('myMessage', array( ->with('myMessage', $this->identicalTo(array(
'{{ value }}' => $value, '{{ value }}' => (string) $value,
'{{ limit }}' => 5, '{{ limit }}' => 5,
), null, 5); )), $this->identicalTo($value), 5);
$this->validator->validate($value, $constraint); $this->validator->validate($value, $constraint);
} }

View File

@ -98,10 +98,10 @@ class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
$this->context->expects($this->once()) $this->context->expects($this->once())
->method('addViolation') ->method('addViolation')
->with('myMessage', array( ->with('myMessage', $this->identicalTo(array(
'{{ value }}' => $value, '{{ value }}' => (string) $value,
'{{ limit }}' => 5, '{{ limit }}' => 5,
), null, 5); )), $this->identicalTo($value), 5);
$this->validator->validate($value, $constraint); $this->validator->validate($value, $constraint);
} }