[Validator] added support for plural messages
This commit is contained in:
parent
1e3028d0d7
commit
345981f6b6
@ -20,14 +20,16 @@ class ConstraintViolation
|
|||||||
{
|
{
|
||||||
protected $messageTemplate;
|
protected $messageTemplate;
|
||||||
protected $messageParameters;
|
protected $messageParameters;
|
||||||
|
protected $messagePluralization;
|
||||||
protected $root;
|
protected $root;
|
||||||
protected $propertyPath;
|
protected $propertyPath;
|
||||||
protected $invalidValue;
|
protected $invalidValue;
|
||||||
|
|
||||||
public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue)
|
public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null)
|
||||||
{
|
{
|
||||||
$this->messageTemplate = $messageTemplate;
|
$this->messageTemplate = $messageTemplate;
|
||||||
$this->messageParameters = $messageParameters;
|
$this->messageParameters = $messageParameters;
|
||||||
|
$this->messagePluralization = $messagePluralization;
|
||||||
$this->root = $root;
|
$this->root = $root;
|
||||||
$this->propertyPath = $propertyPath;
|
$this->propertyPath = $propertyPath;
|
||||||
$this->invalidValue = $invalidValue;
|
$this->invalidValue = $invalidValue;
|
||||||
@ -68,6 +70,14 @@ class ConstraintViolation
|
|||||||
return $this->messageParameters;
|
return $this->messageParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return integer|null
|
||||||
|
*/
|
||||||
|
public function getMessagePluralization()
|
||||||
|
{
|
||||||
|
return $this->messagePluralization;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the violation message.
|
* Returns the violation message.
|
||||||
*
|
*
|
||||||
|
@ -75,13 +75,13 @@ class ChoiceValidator extends ConstraintValidator
|
|||||||
$count = count($value);
|
$count = count($value);
|
||||||
|
|
||||||
if ($constraint->min !== null && $count < $constraint->min) {
|
if ($constraint->min !== null && $count < $constraint->min) {
|
||||||
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min));
|
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($constraint->max !== null && $count > $constraint->max) {
|
if ($constraint->max !== null && $count > $constraint->max) {
|
||||||
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max));
|
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ class MaxLengthValidator extends ConstraintValidator
|
|||||||
$this->context->addViolation($constraint->message, array(
|
$this->context->addViolation($constraint->message, array(
|
||||||
'{{ value }}' => $value,
|
'{{ value }}' => $value,
|
||||||
'{{ limit }}' => $constraint->limit,
|
'{{ limit }}' => $constraint->limit,
|
||||||
));
|
), null, (int) $constraint->limit);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ class MinLengthValidator extends ConstraintValidator
|
|||||||
$this->context->addViolation($constraint->message, array(
|
$this->context->addViolation($constraint->message, array(
|
||||||
'{{ value }}' => $value,
|
'{{ value }}' => $value,
|
||||||
'{{ limit }}' => $constraint->limit,
|
'{{ limit }}' => $constraint->limit,
|
||||||
));
|
), null, (int) $constraint->limit);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -56,10 +56,11 @@ class ExecutionContext
|
|||||||
* @param string $message The error message.
|
* @param string $message The error message.
|
||||||
* @param array $params The parameters parsed into the error message.
|
* @param array $params The parameters parsed into the error message.
|
||||||
* @param mixed $invalidValue The invalid, validated value.
|
* @param mixed $invalidValue The invalid, validated value.
|
||||||
|
* @param integer|null $pluralization The number to use to pluralize of the message.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function addViolation($message, array $params = array(), $invalidValue = null)
|
public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null)
|
||||||
{
|
{
|
||||||
$this->globalContext->addViolation(new ConstraintViolation(
|
$this->globalContext->addViolation(new ConstraintViolation(
|
||||||
$message,
|
$message,
|
||||||
@ -67,7 +68,8 @@ class ExecutionContext
|
|||||||
$this->globalContext->getRoot(),
|
$this->globalContext->getRoot(),
|
||||||
$this->propertyPath,
|
$this->propertyPath,
|
||||||
// check using func_num_args() to allow passing null values
|
// check using func_num_args() to allow passing null values
|
||||||
func_num_args() === 3 ? $invalidValue : $this->value
|
func_num_args() === 3 ? $invalidValue : $this->value,
|
||||||
|
$pluralization
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,8 +81,9 @@ class ExecutionContext
|
|||||||
* @param string $message The error message.
|
* @param string $message The error message.
|
||||||
* @param array $params The parameters parsed into the error message.
|
* @param array $params The parameters parsed into the error message.
|
||||||
* @param mixed $invalidValue The invalid, validated value.
|
* @param mixed $invalidValue The invalid, validated value.
|
||||||
|
* @param integer|null $pluralization The number to use to pluralize of the message.
|
||||||
*/
|
*/
|
||||||
public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null)
|
public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
|
||||||
{
|
{
|
||||||
$this->globalContext->addViolation(new ConstraintViolation(
|
$this->globalContext->addViolation(new ConstraintViolation(
|
||||||
$message,
|
$message,
|
||||||
@ -88,7 +91,8 @@ class ExecutionContext
|
|||||||
$this->globalContext->getRoot(),
|
$this->globalContext->getRoot(),
|
||||||
$propertyPath,
|
$propertyPath,
|
||||||
// check using func_num_args() to allow passing null values
|
// check using func_num_args() to allow passing null values
|
||||||
func_num_args() === 4 ? $invalidValue : $this->value
|
func_num_args() === 4 ? $invalidValue : $this->value,
|
||||||
|
$pluralization
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +104,9 @@ class ExecutionContext
|
|||||||
* @param string $message The error message.
|
* @param string $message The error message.
|
||||||
* @param array $params The parameters parsed into the error message.
|
* @param array $params The parameters parsed into the error message.
|
||||||
* @param mixed $invalidValue The invalid, validated value.
|
* @param mixed $invalidValue The invalid, validated value.
|
||||||
|
* @param integer|null $pluralization The number to use to pluralize of the message.
|
||||||
*/
|
*/
|
||||||
public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null)
|
public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
|
||||||
{
|
{
|
||||||
$this->globalContext->addViolation(new ConstraintViolation(
|
$this->globalContext->addViolation(new ConstraintViolation(
|
||||||
$message,
|
$message,
|
||||||
@ -109,7 +114,8 @@ class ExecutionContext
|
|||||||
$this->globalContext->getRoot(),
|
$this->globalContext->getRoot(),
|
||||||
$this->getPropertyPath($subPath),
|
$this->getPropertyPath($subPath),
|
||||||
// check using func_num_args() to allow passing null values
|
// check using func_num_args() to allow passing null values
|
||||||
func_num_args() === 4 ? $invalidValue : $this->value
|
func_num_args() === 4 ? $invalidValue : $this->value,
|
||||||
|
$pluralization
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->method('addViolation')
|
->method('addViolation')
|
||||||
->with('myMessage', array(
|
->with('myMessage', array(
|
||||||
'{{ value }}' => 'baz',
|
'{{ value }}' => 'baz',
|
||||||
));
|
), null, null);
|
||||||
|
|
||||||
$this->assertFalse($this->validator->isValid('baz', $constraint));
|
$this->assertFalse($this->validator->isValid('baz', $constraint));
|
||||||
}
|
}
|
||||||
@ -196,7 +196,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->method('addViolation')
|
->method('addViolation')
|
||||||
->with('myMessage', array(
|
->with('myMessage', array(
|
||||||
'{{ limit }}' => 2,
|
'{{ limit }}' => 2,
|
||||||
));
|
), null, 2);
|
||||||
|
|
||||||
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
|
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
|
||||||
}
|
}
|
||||||
@ -214,7 +214,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->method('addViolation')
|
->method('addViolation')
|
||||||
->with('myMessage', array(
|
->with('myMessage', array(
|
||||||
'{{ limit }}' => 2,
|
'{{ limit }}' => 2,
|
||||||
));
|
), null, 2);
|
||||||
|
|
||||||
$this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
|
$this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@ class MaxLengthValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('myMessage', array(
|
->with('myMessage', array(
|
||||||
'{{ value }}' => $value,
|
'{{ value }}' => $value,
|
||||||
'{{ limit }}' => 5,
|
'{{ limit }}' => 5,
|
||||||
));
|
), null, 5);
|
||||||
|
|
||||||
$this->assertFalse($this->validator->isValid($value, $constraint));
|
$this->assertFalse($this->validator->isValid($value, $constraint));
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@ class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('myMessage', array(
|
->with('myMessage', array(
|
||||||
'{{ value }}' => $value,
|
'{{ value }}' => $value,
|
||||||
'{{ limit }}' => 5,
|
'{{ limit }}' => 5,
|
||||||
));
|
), null, 5);
|
||||||
|
|
||||||
$this->assertFalse($this->validator->isValid($value, $constraint));
|
$this->assertFalse($this->validator->isValid($value, $constraint));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user