mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
Improve date and time handling on "Max" rule
This commit is contained in:
parent
e91d0ef22e
commit
d6855c0722
|
@ -1406,6 +1406,13 @@ Also accepts dates:
|
|||
v::date()->max('2012-01-01')->validate('2010-01-01'); //true
|
||||
```
|
||||
|
||||
Also date intervals:
|
||||
|
||||
```php
|
||||
// Same of minimum age validation
|
||||
v::date()->max('-18 years')->validate('1988-09-09'); //true
|
||||
```
|
||||
|
||||
`true` may be passed as a parameter to indicate that inclusive
|
||||
values must be used.
|
||||
|
||||
|
|
|
@ -7,12 +7,12 @@ class MaxException extends ValidationException
|
|||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be lower than {{maxValue}}',
|
||||
self::INCLUSIVE => '{{name}} must be lower than or equals {{maxValue}}',
|
||||
self::STANDARD => '{{name}} must be lower than {{interval}}',
|
||||
self::INCLUSIVE => '{{name}} must be lower than or equals {{interval}}',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be lower than {{maxValue}}',
|
||||
self::INCLUSIVE => '{{name}} must not be lower than or equals {{maxValue}}',
|
||||
self::STANDARD => '{{name}} must not be lower than {{interval}}',
|
||||
self::INCLUSIVE => '{{name}} must not be lower than or equals {{interval}}',
|
||||
),
|
||||
);
|
||||
|
||||
|
|
|
@ -1,23 +1,14 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Max extends AbstractRule
|
||||
class Max extends AbstractInterval
|
||||
{
|
||||
public $maxValue;
|
||||
public $inclusive;
|
||||
|
||||
public function __construct($maxValue, $inclusive = false)
|
||||
{
|
||||
$this->maxValue = $maxValue;
|
||||
$this->inclusive = $inclusive;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->inclusive) {
|
||||
return $input <= $this->maxValue;
|
||||
} else {
|
||||
return $input < $this->maxValue;
|
||||
return $this->filterInterval($input) <= $this->filterInterval($this->interval);
|
||||
}
|
||||
|
||||
return $this->filterInterval($input) < $this->filterInterval($this->interval);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,10 @@ class MaxTest extends \PHPUnit_Framework_TestCase
|
|||
array(200, false, -200),
|
||||
array(200, true, 200),
|
||||
array(200, false, 0),
|
||||
array('-18 years', true, '1988-09-09'),
|
||||
array('z', true, 'z'),
|
||||
array('z', false, 'y'),
|
||||
array('tomorrow', true, 'now'),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue