[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');
}
$value = (string) $value;
$stringValue = (string) $value;
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($value);
$length = grapheme_strlen($stringValue);
} elseif (function_exists('mb_strlen')) {
$length = mb_strlen($value, $constraint->charset);
$length = mb_strlen($stringValue, $constraint->charset);
} else {
$length = strlen($value);
$length = strlen($stringValue);
}
if ($length > $constraint->limit) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
'{{ value }}' => $stringValue,
'{{ 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');
}
$value = (string) $value;
$stringValue = (string) $value;
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($value);
$length = grapheme_strlen($stringValue);
} elseif (function_exists('mb_strlen')) {
$length = mb_strlen($value, $constraint->charset);
$length = mb_strlen($stringValue, $constraint->charset);
} else {
$length = strlen($value);
$length = strlen($stringValue);
}
if ($length < $constraint->limit) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
'{{ value }}' => $stringValue,
'{{ 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())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $value,
->with('myMessage', $this->identicalTo(array(
'{{ value }}' => (string) $value,
'{{ limit }}' => 5,
), null, 5);
)), $this->identicalTo($value), 5);
$this->validator->validate($value, $constraint);
}

View File

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