Skip to content

Commit 57b31fc

Browse files
bscheshirworksamdark
authored andcommitted
Added example for yii\validators\Validator::addError() [skip ci] (yiisoft#15197)
1 parent 5483a2e commit 57b31fc

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

docs/guide-ru/input-validation.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ class MyForm extends Model
358358

359359
public function validateCountry($attribute, $params)
360360
{
361-
if (!in_array($this->$attribute, ['USA', 'Web'])) {
362-
$this->addError($attribute, 'Страна должна быть либо "USA" или "Web".');
361+
if (!in_array($this->$attribute, ['USA', 'Indonesia'])) {
362+
$this->addError($attribute, 'Страна должна быть либо "USA" или "Indonesia".');
363363
}
364364
}
365365
}
@@ -384,7 +384,9 @@ class MyForm extends Model
384384
Вы можете реализовать свою логику проверки путем переопределения метода
385385
[[yii\validators\Validator::validateAttribute()]]. Если атрибут не прошел проверку, вызвать
386386
[[yii\base\Model::addError()]],
387-
чтобы сохранить сообщение об ошибке в модели, как это делают [встроенные валидаторы](#inline-validators). Например:
387+
чтобы сохранить сообщение об ошибке в модели, как это делают [встроенные валидаторы](#inline-validators).
388+
389+
Валидация может быть помещена в отдельный класс [[components/validators/CountryValidator]]. В этом случае можно использовать метод [[yii\validators\Validator::addError()]] для того, чтобы добавить своё сообщение об ошибке в модель:
388390
389391
```php
390392
namespace app\components;
@@ -395,8 +397,8 @@ class CountryValidator extends Validator
395397
{
396398
public function validateAttribute($model, $attribute)
397399
{
398-
if (!in_array($model->$attribute, ['USA', 'Web'])) {
399-
$this->addError($model, $attribute, 'Страна должна быть либо "USA" или "Web".');
400+
if (!in_array($model->$attribute, ['USA', 'Indonesia'])) {
401+
$this->addError($model, $attribute, 'Страна должна быть либо "{country1}" либо "{country2}".', ['country1' => 'USA', 'country2' => 'Indonesia']);
400402
}
401403
}
402404
}

docs/guide/input-validation.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ class MyForm extends Model
387387

388388
public function validateCountry($attribute, $params, $validator)
389389
{
390-
if (!in_array($this->$attribute, ['USA', 'Web'])) {
391-
$this->addError($attribute, 'The country must be either "USA" or "Web".');
390+
if (!in_array($this->$attribute, ['USA', 'Indonesia'])) {
391+
$this->addError($attribute, 'The country must be either "USA" or "Indonesia".');
392392
}
393393
}
394394
}
@@ -422,7 +422,8 @@ fails the validation, call [[yii\base\Model::addError()]] to save the error mess
422422
with [inline validators](#inline-validators).
423423
424424
425-
For example the inline validator above could be moved into new [[components/validators/CountryValidator]] class.
425+
For example, the inline validator above could be moved into new [[components/validators/CountryValidator]] class.
426+
In this case we can use [[yii\validators\Validator::addError()]] to set customized message for the model.
426427
427428
```php
428429
namespace app\components;
@@ -433,8 +434,8 @@ class CountryValidator extends Validator
433434
{
434435
public function validateAttribute($model, $attribute)
435436
{
436-
if (!in_array($model->$attribute, ['USA', 'Web'])) {
437-
$this->addError($model, $attribute, 'The country must be either "USA" or "Web".');
437+
if (!in_array($model->$attribute, ['USA', 'Indonesia'])) {
438+
$this->addError($model, $attribute, 'The country must be either "{country1}" or "{country2}".', ['country1' => 'USA', 'country2' => 'Indonesia']);
438439
}
439440
}
440441
}

0 commit comments

Comments
 (0)