Added min/max option to DateValidator.

Refs #7514
close #8115
This commit is contained in:
Nikola Kovacs
2015-04-17 14:54:40 +02:00
committed by Carsten Brandt
parent cf51d46a75
commit 3f24954659
3 changed files with 192 additions and 4 deletions

View File

@ -7,11 +7,11 @@
namespace yii\validators;
use DateTimeInterface;
use DateTime;
use IntlDateFormatter;
use Yii;
use DateTime;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\helpers\FormatConverter;
/**
@ -90,6 +90,44 @@ class DateValidator extends Validator
* @since 2.0.4
*/
public $timestampAttributeTimeZone = 'UTC';
/**
* @var integer|string upper limit of the date. Defaults to null, meaning no upper limit.
* This can be a unix timestamp or a string representing a date time value.
* If this property is a string, [[format]] will be used to parse it.
* @see tooBig for the customized message used when the date is too big.
* @since 2.0.4
*/
public $max;
/**
* @var integer|string lower limit of the date. Defaults to null, meaning no lower limit.
* This can be a unix timestamp or a string representing a date time value.
* If this property is a string, [[format]] will be used to parse it.
* @see tooSmall for the customized message used when the date is too small.
* @since 2.0.4
*/
public $min;
/**
* @var string user-defined error message used when the value is bigger than [[max]].
* @since 2.0.4
*/
public $tooBig;
/**
* @var string user-defined error message used when the value is smaller than [[min]].
* @since 2.0.4
*/
public $tooSmall;
/**
* @var string user friendly value of upper limit to display in the error message.
* If this property is null, the value of [[max]] will be used (before parsing).
* @since 2.0.4
*/
public $maxString;
/**
* @var string user friendly value of lower limit to display in the error message.
* If this property is null, the value of [[min]] will be used (before parsing).
* @since 2.0.4
*/
public $minString;
/**
* @var array map of short format names to IntlDateFormatter constant values.
@ -120,6 +158,32 @@ class DateValidator extends Validator
if ($this->timeZone === null) {
$this->timeZone = Yii::$app->timeZone;
}
if ($this->min !== null && $this->tooSmall === null) {
$this->tooSmall = Yii::t('yii', '{attribute} must be no less than {min}.');
}
if ($this->max !== null && $this->tooBig === null) {
$this->tooBig = Yii::t('yii', '{attribute} must be no greater than {max}.');
}
if ($this->maxString === null) {
$this->maxString = (string)$this->max;
}
if ($this->minString === null) {
$this->minString = (string)$this->min;
}
if ($this->max !== null && is_string($this->max)) {
$timestamp = $this->parseDateValue($this->max);
if ($timestamp === false) {
throw new InvalidConfigException("Invalid max date value: {$this->max}");
}
$this->max = $timestamp;
}
if ($this->min !== null && is_string($this->min)) {
$timestamp = $this->parseDateValue($this->min);
if ($timestamp === false) {
throw new InvalidConfigException("Invalid min date value: {$this->min}");
}
$this->min = $timestamp;
}
}
/**
@ -131,6 +195,10 @@ class DateValidator extends Validator
$timestamp = $this->parseDateValue($value);
if ($timestamp === false) {
$this->addError($model, $attribute, $this->message, []);
} elseif ($this->min !== null && $timestamp < $this->min) {
$this->addError($model, $attribute, $this->tooSmall, ['min' => $this->minString]);
} elseif ($this->max !== null && $timestamp > $this->max) {
$this->addError($model, $attribute, $this->tooBig, ['max' => $this->maxString]);
} elseif ($this->timestampAttribute !== null) {
if ($this->timestampAttributeFormat === null) {
$model->{$this->timestampAttribute} = $timestamp;
@ -145,14 +213,23 @@ class DateValidator extends Validator
*/
protected function validateValue($value)
{
return $this->parseDateValue($value) === false ? [$this->message, []] : null;
$timestamp = $this->parseDateValue($value);
if ($timestamp === false) {
return [$this->message, []];
} elseif ($this->min !== null && $timestamp < $this->min) {
return [$this->tooSmall, ['min' => $this->minString]];
} elseif ($this->max !== null && $timestamp > $this->max) {
return [$this->tooBig, ['max' => $this->maxString]];
} else {
return null;
}
}
/**
* Parses date string into UNIX timestamp
*
* @param string $value string representing date
* @return integer|boolean a UNIX timestamp or `false` on failure.
* @return integer|false a UNIX timestamp or `false` on failure.
*/
protected function parseDateValue($value)
{