Better message for Length rule

Update exception message when minimum value and maximum value are the
same.
This commit is contained in:
Henrique Moody 2017-02-12 18:14:38 +01:00
parent d14a87c6c7
commit b43c9b103b
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 26 additions and 1 deletions

View file

@ -16,17 +16,20 @@ class LengthException extends ValidationException
const BOTH = 0;
const LOWER = 1;
const GREATER = 2;
const EXACT = 3;
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must have a length greater than {{minValue}}',
self::GREATER => '{{name}} must have a length lower than {{maxValue}}',
self::EXACT => '{{name}} must have a length of {{maxValue}}',
],
self::MODE_NEGATIVE => [
self::BOTH => '{{name}} must not have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must not have a length greater than {{minValue}}',
self::GREATER => '{{name}} must not have a length lower than {{maxValue}}',
self::EXACT => '{{name}} must not have a length of {{maxValue}}',
],
];
@ -34,10 +37,16 @@ class LengthException extends ValidationException
{
if (!$this->getParam('minValue')) {
return static::GREATER;
} elseif (!$this->getParam('maxValue')) {
}
if (!$this->getParam('maxValue')) {
return static::LOWER;
}
if ($this->getParam('minValue') == $this->getParam('maxValue')) {
return self::EXACT;
}
return static::BOTH;
}
}

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\LengthException;
use Respect\Validation\Validator as v;
try {
v::length(5, 5)->check('123456');
} catch (LengthException $e) {
echo $e->getMainMessage();
}
?>
--EXPECTF--
"123456" must have a length of 5