[Form] Fixed invalid value passed to the constraint violation by the MinLength and MaxLength validators
This commit is contained in:
parent
35e8d086ed
commit
f093caa6ac
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user