mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fixes #7566: Improved \yii\validators\CompareValidator
default messages
This commit is contained in:

committed by
Alexander Makarov

parent
bb2bc7008d
commit
c33574bc0a
@ -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)
|
||||
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user