mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-14 13:25:23 +08:00
Merge pull request #11150 from yiisoft/11139-each-validator-attribute-value
EachValidator injects specific attribute value in error message
This commit is contained in:
@@ -29,7 +29,8 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #10610: Added `BaseUrl::$urlManager` to be able to set URL manager used for creating URLs (samdark)
|
- Enh #10610: Added `BaseUrl::$urlManager` to be able to set URL manager used for creating URLs (samdark)
|
||||||
- Enh #10764: `yii\helpers\Html::tag()` and `::beginTag()` return content without any HTML when the `$tag` attribute is `false` or `null` (pana1990)
|
- Enh #10764: `yii\helpers\Html::tag()` and `::beginTag()` return content without any HTML when the `$tag` attribute is `false` or `null` (pana1990)
|
||||||
- Enh #10941: Added `yii\helpers\ArrayHelper::isTraversable`, added support for traversable selections for dropdownList, radioList and checkboxList in `yii\helpers\Html` (sammousa)
|
- Enh #10941: Added `yii\helpers\ArrayHelper::isTraversable`, added support for traversable selections for dropdownList, radioList and checkboxList in `yii\helpers\Html` (sammousa)
|
||||||
- Eng #10976: `Inflector::transliterate()` now uses `strtr` instead of `str_replace` (DrDeath72)
|
- Enh #10976: `Inflector::transliterate()` now uses `strtr` instead of `str_replace` (DrDeath72)
|
||||||
|
- Enh #11139: `yii\validators\EachValidator` injects specific attribute value in error message parameters (silverfire)
|
||||||
- Enh: Added `StringHelper::countWords()` that given a string returns number of words in it (samdark)
|
- Enh: Added `StringHelper::countWords()` that given a string returns number of words in it (samdark)
|
||||||
- Bug #11040: Check parameter 'recursive' and disable recursive copying with option 'recursive' => false in method BaseFileHelper::copyDirectory (Ni-san)
|
- Bug #11040: Check parameter 'recursive' and disable recursive copying with option 'recursive' => false in method BaseFileHelper::copyDirectory (Ni-san)
|
||||||
- Chg: HTMLPurifier dependency updated to `~4.6` (samdark)
|
- Chg: HTMLPurifier dependency updated to `~4.6` (samdark)
|
||||||
|
|||||||
@@ -146,7 +146,12 @@ class EachValidator extends Validator
|
|||||||
}
|
}
|
||||||
$result = $validator->validateValue($v);
|
$result = $validator->validateValue($v);
|
||||||
if ($result !== null) {
|
if ($result !== null) {
|
||||||
return $this->allowMessageFromRule ? $result : [$this->message, []];
|
if ($this->allowMessageFromRule) {
|
||||||
|
$result[1]['value'] = $v;
|
||||||
|
return $result;
|
||||||
|
} else {
|
||||||
|
return [$this->message, ['value' => $v]];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -356,9 +356,11 @@ class Validator extends Component
|
|||||||
*/
|
*/
|
||||||
public function addError($model, $attribute, $message, $params = [])
|
public function addError($model, $attribute, $message, $params = [])
|
||||||
{
|
{
|
||||||
$value = $model->$attribute;
|
|
||||||
$params['attribute'] = $model->getAttributeLabel($attribute);
|
$params['attribute'] = $model->getAttributeLabel($attribute);
|
||||||
$params['value'] = is_array($value) ? 'array()' : $value;
|
if (!isset($params['value'])) {
|
||||||
|
$value = $model->$attribute;
|
||||||
|
$params['value'] = is_array($value) ? 'array()' : $value;
|
||||||
|
}
|
||||||
$model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
|
$model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,28 @@ class EachValidatorTest extends TestCase
|
|||||||
$this->assertNotContains('integer', $model->getFirstError('attr_one'));
|
$this->assertNotContains('integer', $model->getFirstError('attr_one'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testValidate
|
||||||
|
*/
|
||||||
|
public function testCustomMessageValue()
|
||||||
|
{
|
||||||
|
$model = FakedValidationModel::createWithAttributes([
|
||||||
|
'attr_one' => [
|
||||||
|
'TEXT',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
$validator = new EachValidator(['rule' => ['integer', 'message' => '{value} is not an integer']]);
|
||||||
|
|
||||||
|
$validator->validateAttribute($model, 'attr_one');
|
||||||
|
$this->assertSame('TEXT is not an integer', $model->getFirstError('attr_one'));
|
||||||
|
|
||||||
|
$model->clearErrors();
|
||||||
|
$validator->allowMessageFromRule = false;
|
||||||
|
$validator->message = '{value} is invalid';
|
||||||
|
$validator->validateAttribute($model, 'attr_one');
|
||||||
|
$this->assertEquals('TEXT is invalid', $model->getFirstError('attr_one'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://github.com/yiisoft/yii2/issues/10825
|
* @see https://github.com/yiisoft/yii2/issues/10825
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user