Current Path : /home/vmanager/www/common/components/validators/ |
Linux 9dbcd5f6333d 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64 |
Current File : /home/vmanager/www/common/components/validators/CountValidator.php |
<?php namespace common\components\validators; use Yii; use yii\validators\Validator; class CountValidator extends Validator { public $min; public $max; public $notEnough; public $tooMuch; public $message; /** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('common-validators', '{attribute} musi być listą.'); } if ($this->min !== null && $this->notEnough === null) { $this->notEnough = Yii::t('common-validators', '{attribute} musi zawierać przynajmniej {min} elementów.'); } if ($this->max !== null && $this->tooMuch === null) { $this->tooMuch = Yii::t('common-validators', '{attribute} może zawierać maksymalnie {max} elementów.'); } } /** * @inheritdoc */ public function validateAttribute($model, $attribute) { $value = $model->$attribute; if (!is_array($value)) { $this->addError($model, $attribute, $this->message); return; } $count = count($value); if ($this->min !== null && $count < $this->min) { $this->addError($model, $attribute, $this->notEnough, ['min' => $this->min]); } if ($this->max !== null && $count > $this->max) { $this->addError($model, $attribute, $this->tooMuch, ['max' => $this->max]); } } /** * @inheritdoc */ protected function validateValue($value) { if (!is_array($value)) { return [$this->message, []]; } $count = count($value); if ($this->min !== null && $count < $this->min) { return [$this->notEnough, ['min' => $this->min]]; } if ($this->max !== null && $count > $this->max) { return [$this->tooMuch, ['max' => $this->max]]; } return null; } }