mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
Fixes #13087: Fixed getting active validators for safe attribute
This commit is contained in:
committed by
Alexander Makarov
parent
0ea45c78a8
commit
2de18cf9a5
@ -101,6 +101,11 @@ class Validator extends Component
|
||||
* please specify them as an array; for single attribute, you may use either a string or an array.
|
||||
*/
|
||||
public $attributes = [];
|
||||
/**
|
||||
* @var array cleaned attribute names. Contains attribute names without `!` character at the beginning
|
||||
* @since 2.0.12
|
||||
*/
|
||||
private $attributeNames = [];
|
||||
/**
|
||||
* @var string the user-defined error message. It may contain the following placeholders which
|
||||
* will be replaced accordingly by the validator:
|
||||
@ -233,30 +238,29 @@ class Validator extends Component
|
||||
$this->attributes = (array) $this->attributes;
|
||||
$this->on = (array) $this->on;
|
||||
$this->except = (array) $this->except;
|
||||
$this->setAttributeNames((array)$this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the specified object.
|
||||
* @param \yii\base\Model $model the data model being validated
|
||||
* @param array|null $attributes the list of attributes to be validated.
|
||||
* Note that if an attribute is not associated with the validator, or is is prefixed with `!` char - it will be
|
||||
* Note that if an attribute is not associated with the validator - it will be
|
||||
* ignored. If this parameter is null, every attribute listed in [[attributes]] will be validated.
|
||||
* @since 2.0.12
|
||||
*/
|
||||
public function validateAttributes($model, $attributes = null)
|
||||
{
|
||||
if (is_array($attributes)) {
|
||||
$newAttributes = [];
|
||||
foreach ($attributes as $attribute) {
|
||||
if (in_array($attribute, $this->attributes) || in_array('!' . $attribute, $this->attributes)) {
|
||||
if (in_array($attribute, $this->getAttributeNames(), true)) {
|
||||
$newAttributes[] = $attribute;
|
||||
}
|
||||
}
|
||||
$attributes = $newAttributes;
|
||||
} else {
|
||||
$attributes = [];
|
||||
foreach ($this->attributes as $attribute) {
|
||||
$attributes[] = $attribute[0] === '!' ? substr($attribute, 1) : $attribute;
|
||||
}
|
||||
$attributes = $this->getAttributeNames();
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
@ -432,4 +436,26 @@ class Validator extends Component
|
||||
return $value === null || $value === [] || $value === '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns cleaned attribute names without the `!` character at the beginning
|
||||
* @return array
|
||||
* @since 2.0.12
|
||||
*/
|
||||
public function getAttributeNames()
|
||||
{
|
||||
return $this->attributeNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves attribute names without `!` character at the beginning
|
||||
* @param array $attributeNames
|
||||
* @since 2.0.12
|
||||
*/
|
||||
private function setAttributeNames($attributeNames)
|
||||
{
|
||||
$this->attributeNames = array_map(function($attribute) {
|
||||
return ltrim($attribute, '!');
|
||||
}, $attributeNames);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user