mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 05:48:11 +08:00
Merge branch 'master' into fix-numbervalidator-comma-decimal-separator
This commit is contained in:
@ -60,6 +60,15 @@ class EachValidator extends Validator
|
||||
* If disabled, own error message value will be used always.
|
||||
*/
|
||||
public $allowMessageFromRule = true;
|
||||
/**
|
||||
* @var bool whether to stop validation once first error among attribute value elements is detected.
|
||||
* When enabled validation will produce single error message on attribute, when disabled - multiple
|
||||
* error messages mya appear: one per each invalid value.
|
||||
* Note that this option will affect only [[validateAttribute()]] value, while [[validateValue()]] will
|
||||
* not be affected.
|
||||
* @since 2.0.11
|
||||
*/
|
||||
public $stopOnFirstError = true;
|
||||
|
||||
/**
|
||||
* @var Validator validator instance.
|
||||
@ -125,30 +134,35 @@ class EachValidator extends Validator
|
||||
|
||||
$validator = $this->getValidator($model); // ensure model context while validator creation
|
||||
|
||||
$originalErrors = $model->getErrors($attribute);
|
||||
$filteredValue = [];
|
||||
$detectedErrors = $model->getErrors($attribute);
|
||||
$filteredValue = $model->$attribute;
|
||||
foreach ($value as $k => $v) {
|
||||
$model->clearErrors($attribute);
|
||||
$model->$attribute = $v;
|
||||
if (!$validator->skipOnEmpty || !$validator->isEmpty($v)) {
|
||||
$validator->validateAttribute($model, $attribute);
|
||||
}
|
||||
$filteredValue[$k] = $model->$attribute;
|
||||
if ($model->hasErrors($attribute)) {
|
||||
$validationErrors = $model->getErrors($attribute);
|
||||
$model->clearErrors($attribute);
|
||||
if (!empty($originalErrors)) {
|
||||
$model->addErrors([$attribute => $originalErrors]);
|
||||
}
|
||||
if ($this->allowMessageFromRule) {
|
||||
$model->addErrors([$attribute => $validationErrors]);
|
||||
$validationErrors = $model->getErrors($attribute);
|
||||
$detectedErrors = array_merge($detectedErrors, $validationErrors);
|
||||
} else {
|
||||
$model->clearErrors($attribute);
|
||||
$this->addError($model, $attribute, $this->message, ['value' => $v]);
|
||||
$detectedErrors[] = $model->getFirstError($attribute);
|
||||
}
|
||||
$model->$attribute = $value;
|
||||
return;
|
||||
|
||||
if ($this->stopOnFirstError) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$model->$attribute = $filteredValue;
|
||||
$model->clearErrors($attribute);
|
||||
$model->addErrors([$attribute => $detectedErrors]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -35,7 +35,7 @@ class InlineValidator extends Validator
|
||||
*
|
||||
* - `$attribute` is the name of the attribute to be validated;
|
||||
* - `$params` contains the value of [[params]] that you specify when declaring the inline validation rule;
|
||||
* - `$validator` is a reference to related [[InlineValidator]] object.
|
||||
* - `$validator` is a reference to related [[InlineValidator]] object. This parameter is available since version 2.0.11.
|
||||
*/
|
||||
public $method;
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user