mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-22 06:57:12 +01:00
merge from yiisoft/yii2
This commit is contained in:
@@ -240,24 +240,13 @@ class Validator extends Component
|
||||
/**
|
||||
* 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.
|
||||
* @param array|string|null $attributes the list of attributes to be validated.
|
||||
* 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.
|
||||
*/
|
||||
public function validateAttributes($model, $attributes = null)
|
||||
{
|
||||
if (is_array($attributes)) {
|
||||
$newAttributes = [];
|
||||
$attributeNames = $this->getAttributeNames();
|
||||
foreach ($attributes as $attribute) {
|
||||
if (in_array($attribute, $attributeNames, true)) {
|
||||
$newAttributes[] = $attribute;
|
||||
}
|
||||
}
|
||||
$attributes = $newAttributes;
|
||||
} else {
|
||||
$attributes = $this->getAttributeNames();
|
||||
}
|
||||
$attributes = $this->getValidationAttributes($attributes);
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
$skip = $this->skipOnError && $model->hasErrors($attribute)
|
||||
@@ -270,6 +259,26 @@ class Validator extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function getValidationAttributes($attributes = null)
|
||||
{
|
||||
if ($attributes === null) {
|
||||
return $this->getAttributeNames();
|
||||
}
|
||||
|
||||
if (is_string($attributes)) {
|
||||
$attributes = [$attributes];
|
||||
}
|
||||
|
||||
$newAttributes = [];
|
||||
$attributeNames = $this->getAttributeNames();
|
||||
foreach ($attributes as $attribute) {
|
||||
if (in_array($attribute, $attributeNames, true)) {
|
||||
$newAttributes[] = $attribute;
|
||||
}
|
||||
}
|
||||
return $newAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a single attribute.
|
||||
* Child classes must implement this method to provide the actual validation logic.
|
||||
|
||||
Reference in New Issue
Block a user