123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace yii\validators;
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\Html;
- class CompareValidator extends Validator
- {
-
- const TYPE_STRING = 'string';
-
- const TYPE_NUMBER = 'number';
-
- public $compareAttribute;
-
- public $compareValue;
-
- public $type = self::TYPE_STRING;
-
- public $operator = '==';
-
- public $message;
-
- public function init()
- {
- parent::init();
- if ($this->message === null) {
- switch ($this->operator) {
- case '==':
- $this->message = Yii::t('yii', '{attribute} must be equal to "{compareValueOrAttribute}".');
- break;
- case '===':
- $this->message = Yii::t('yii', '{attribute} must be equal to "{compareValueOrAttribute}".');
- break;
- case '!=':
- $this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValueOrAttribute}".');
- break;
- case '!==':
- $this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValueOrAttribute}".');
- break;
- case '>':
- $this->message = Yii::t('yii', '{attribute} must be greater than "{compareValueOrAttribute}".');
- break;
- case '>=':
- $this->message = Yii::t('yii', '{attribute} must be greater than or equal to "{compareValueOrAttribute}".');
- break;
- case '<':
- $this->message = Yii::t('yii', '{attribute} must be less than "{compareValueOrAttribute}".');
- break;
- case '<=':
- $this->message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValueOrAttribute}".');
- break;
- default:
- throw new InvalidConfigException("Unknown operator: {$this->operator}");
- }
- }
- }
-
- public function validateAttribute($model, $attribute)
- {
- $value = $model->$attribute;
- if (is_array($value)) {
- $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
- return;
- }
- if ($this->compareValue !== null) {
- $compareLabel = $compareValue = $compareValueOrAttribute = $this->compareValue;
- } else {
- $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
- $compareValue = $model->$compareAttribute;
- $compareLabel = $compareValueOrAttribute = $model->getAttributeLabel($compareAttribute);
- }
- if (!$this->compareValues($this->operator, $this->type, $value, $compareValue)) {
- $this->addError($model, $attribute, $this->message, [
- 'compareAttribute' => $compareLabel,
- 'compareValue' => $compareValue,
- 'compareValueOrAttribute' => $compareValueOrAttribute,
- ]);
- }
- }
-
- protected function validateValue($value)
- {
- if ($this->compareValue === null) {
- throw new InvalidConfigException('CompareValidator::compareValue must be set.');
- }
- if (!$this->compareValues($this->operator, $this->type, $value, $this->compareValue)) {
- return [$this->message, [
- 'compareAttribute' => $this->compareValue,
- 'compareValue' => $this->compareValue,
- 'compareValueOrAttribute' => $this->compareValue,
- ]];
- }
- return null;
- }
-
- protected function compareValues($operator, $type, $value, $compareValue)
- {
- if ($type === self::TYPE_NUMBER) {
- $value = (float) $value;
- $compareValue = (float) $compareValue;
- } else {
- $value = (string) $value;
- $compareValue = (string) $compareValue;
- }
- switch ($operator) {
- case '==':
- return $value == $compareValue;
- case '===':
- return $value === $compareValue;
- case '!=':
- return $value != $compareValue;
- case '!==':
- return $value !== $compareValue;
- case '>':
- return $value > $compareValue;
- case '>=':
- return $value >= $compareValue;
- case '<':
- return $value < $compareValue;
- case '<=':
- return $value <= $compareValue;
- default:
- return false;
- }
- }
-
- public function clientValidateAttribute($model, $attribute, $view)
- {
- ValidationAsset::register($view);
- $options = $this->getClientOptions($model, $attribute);
- return 'yii.validation.compare(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ', $form);';
- }
-
- public function getClientOptions($model, $attribute)
- {
- $options = [
- 'operator' => $this->operator,
- 'type' => $this->type,
- ];
- if ($this->compareValue !== null) {
- $options['compareValue'] = $this->compareValue;
- $compareLabel = $compareValue = $compareValueOrAttribute = $this->compareValue;
- } else {
- $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
- $compareValue = $model->getAttributeLabel($compareAttribute);
- $options['compareAttribute'] = Html::getInputId($model, $compareAttribute);
- $options['compareAttributeName'] = Html::getInputName($model, $compareAttribute);
- $compareLabel = $compareValueOrAttribute = $model->getAttributeLabel($compareAttribute);
- }
- if ($this->skipOnEmpty) {
- $options['skipOnEmpty'] = 1;
- }
- $options['message'] = $this->formatMessage($this->message, [
- 'attribute' => $model->getAttributeLabel($attribute),
- 'compareAttribute' => $compareLabel,
- 'compareValue' => $compareValue,
- 'compareValueOrAttribute' => $compareValueOrAttribute,
- ]);
- return $options;
- }
- }
|