From c33574bc0aeec8de4089acef224bee25145ab2ce Mon Sep 17 00:00:00 2001 From: "Sidney Lins (slinstj, former sidtj)" Date: Sat, 21 Mar 2015 14:47:35 -0300 Subject: [PATCH] Fixes #7566: Improved `\yii\validators\CompareValidator` default messages --- framework/CHANGELOG.md | 1 + framework/validators/CompareValidator.php | 21 ++++----- .../validators/CompareValidatorTest.php | 44 +++++++++++++++++++ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5a28bad546..85b9ea2f89 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -36,6 +36,7 @@ Yii Framework 2 Change Log - Enh #3506: Added `\yii\validators\IpValidator` to perform validation of IP addresses and subnets (SilverFire, samdark) - Enh #5146: Added `\yii\i18n\Formatter::asDuration()` method (nineinchnick, SilverFire) - Enh #7341: Client validation now skips disabled inputs (SamMousa) +- Enh #7566: Improved `\yii\validators\CompareValidator` default messages (slinstj) - Enh #7581: Added ability to specify range using anonymous function in `RangeValidator` (RomeroMsk) - Enh #8613: `yii\widgets\FragmentCache` will not store empty content anymore which fixes some problems related to `yii\filters\PageCache` (kidol) - Enh #8649: Added total applied migrations to final report (vernik91) diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 98e6d9cefe..b6af707f16 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -87,28 +87,28 @@ class CompareValidator extends Validator if ($this->message === null) { switch ($this->operator) { case '==': - $this->message = Yii::t('yii', '{attribute} must be repeated exactly.'); + $this->message = Yii::t('yii', '{attribute} must be equal to "{compareValueOrAttribute}".'); break; case '===': - $this->message = Yii::t('yii', '{attribute} must be repeated exactly.'); + $this->message = Yii::t('yii', '{attribute} must be equal to "{compareValueOrAttribute}".'); break; case '!=': - $this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); + $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 "{compareValue}".'); + $this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValueOrAttribute}".'); break; case '>': - $this->message = Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); + $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 "{compareValue}".'); + $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 "{compareValue}".'); + $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 "{compareValue}".'); + $this->message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValueOrAttribute}".'); break; default: throw new InvalidConfigException("Unknown operator: {$this->operator}"); @@ -128,17 +128,18 @@ class CompareValidator extends Validator return; } if ($this->compareValue !== null) { - $compareLabel = $compareValue = $this->compareValue; + $compareLabel = $compareValue = $compareValueOrAttribute = $this->compareValue; } else { $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; $compareValue = $model->$compareAttribute; - $compareLabel = $model->getAttributeLabel($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, ]); } } diff --git a/tests/framework/validators/CompareValidatorTest.php b/tests/framework/validators/CompareValidatorTest.php index 239be30bfe..5a09cc3bde 100644 --- a/tests/framework/validators/CompareValidatorTest.php +++ b/tests/framework/validators/CompareValidatorTest.php @@ -141,6 +141,50 @@ class CompareValidatorTest extends TestCase $this->assertTrue($model->hasErrors('attr_o')); } + public function testAttributeErrorMessages() + { + $model = FakedValidationModel::createWithAttributes([ + 'attr1' => 1, + 'attr2' => 2, + 'attrN' => 2, + ]); + + foreach ($this->getTestDataForMessages() as $data) { + $model->clearErrors($data[0]); + $validator = new CompareValidator(); + $validator->operator = $data[1]; + $validator->message = null; + $validator->init(); // reload messages + $validator->$data[4] = $data[2]; + $validator->validateAttribute($model, $data[0]); + $error = $model->getErrors($data[0])[0]; + $this->assertEquals($data[3], $error); + } + } + + protected function getTestDataForMessages() + { + return [ + ['attr1', '==', 2, 'attr1 must be equal to "2".', 'compareValue'], + ['attr1', '===', 2, 'attr1 must be equal to "2".', 'compareValue'], + ['attrN', '!=', 2, 'attrN must not be equal to "2".', 'compareValue'], + ['attrN', '!==', 2, 'attrN must not be equal to "2".', 'compareValue'], + ['attr1', '>', 2, 'attr1 must be greater than "2".', 'compareValue'], + ['attr1', '>=', 2, 'attr1 must be greater than or equal to "2".', 'compareValue'], + ['attr2', '<', 1, 'attr2 must be less than "1".', 'compareValue'], + ['attr2', '<=', 1, 'attr2 must be less than or equal to "1".', 'compareValue'], + + ['attr1', '==', 'attr2', 'attr1 must be equal to "attr2".', 'compareAttribute'], + ['attr1', '===', 'attr2', 'attr1 must be equal to "attr2".', 'compareAttribute'], + ['attrN', '!=', 'attr2', 'attrN must not be equal to "attr2".', 'compareAttribute'], + ['attrN', '!==', 'attr2', 'attrN must not be equal to "attr2".', 'compareAttribute'], + ['attr1', '>', 'attr2', 'attr1 must be greater than "attr2".', 'compareAttribute'], + ['attr1', '>=', 'attr2', 'attr1 must be greater than or equal to "attr2".', 'compareAttribute'], + ['attr2', '<', 'attr1', 'attr2 must be less than "attr1".', 'compareAttribute'], + ['attr2', '<=', 'attr1', 'attr2 must be less than or equal to "attr1".', 'compareAttribute'], + ]; + } + public function testValidateAttributeOperators() { $value = 55;