/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/validators/IntervalValidator.php
70 строк
2 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\validators; use yii\base\InvalidConfigException; use yii\validators\Validator; use Yii; /** * Валидатор интервалов дат * -- дата начала не может быть позднее чем дата окончания * -- дата окончания не может быть раньше чем дата начала * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019 */ class IntervalValidator extends Validator { /** * Название атрибуда с которым сравниваем дату начала * @var null */ public $compareAttribute = null; /** * Направление проверки * @var string */ public $direction = 'begin'; // begin - проверяем дату начала, end - проверяем дату окончания /** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { if ($this->direction == 'begin') { $this->message = Yii::t('app', 'Дата начала не может быть позднее чем дата окончания'); } else { $this->message = Yii::t('app', 'Дата окончания должна быть позднее чем дата начала'); } } if ($this->compareAttribute === null) { throw new InvalidConfigException('IntervalValidator::compareValue must be set.'); } } /** * @inheritdoc */ public function validateAttribute($model, $attribute) { if ($this->direction == 'begin') { $date_begin = $model->{$attribute}; $date_end = $model->{$this->compareAttribute}; } else { $date_end = $model->{$attribute}; $date_begin = $model->{$this->compareAttribute}; } if ($date_begin and $date_end) { if (strtotime($date_begin) > strtotime($date_end)) { $this->addError($model, $attribute, $this->message); } } } }