Skip to content

Commit

Permalink
Fixes yiisoft#13087: Fixed getting active validators for safe attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
developeruz authored and samdark committed Feb 22, 2017
1 parent 0ea45c7 commit 2de18cf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 Change Log
2.0.12 under development
--------------------------


- Bug #13087: Fixed getting active validators for safe attribute (developeruz)
- Bug #13571: Fix `yii\db\mssql\QueryBuilder::checkIntegrity` for all tables (boboldehampsink)
- Bug #11230: Include `defaultRoles` in `yii\rbac\DbManager->getRolesByUser()` results (developeruz)
- Bug #11404: `yii\base\Model::loadMultiple()` returns true even if `yii\base\Model::load()` returns false (zvook)
Expand Down
2 changes: 1 addition & 1 deletion framework/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public function getActiveValidators($attribute = null)
$validators = [];
$scenario = $this->getScenario();
foreach ($this->getValidators() as $validator) {
if ($validator->isActive($scenario) && ($attribute === null || in_array($attribute, $validator->attributes, true))) {
if ($validator->isActive($scenario) && ($attribute === null || in_array($attribute, $validator->getAttributeNames(), true))) {
$validators[] = $validator;
}
}
Expand Down
38 changes: 32 additions & 6 deletions framework/validators/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -233,30 +238,29 @@ public function init()
$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) {
Expand Down Expand Up @@ -432,4 +436,26 @@ public function isEmpty($value)
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);
}
}
4 changes: 3 additions & 1 deletion tests/data/validators/models/FakedValidationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class FakedValidationModel extends Model
public $val_attr_b;
public $val_attr_c;
public $val_attr_d;
public $safe_attr;
private $attr = [];
private $inlineValArgs;

Expand All @@ -33,7 +34,8 @@ public function rules()
[['val_attr_a', 'val_attr_b'], 'required', 'on' => 'reqTest'],
['val_attr_c', 'integer'],
['attr_images', 'file', 'maxFiles' => 3, 'extensions' => ['png'], 'on' => 'validateMultipleFiles', 'checkExtensionByMimeType' => false],
['attr_image', 'file', 'extensions' => ['png'], 'on' => 'validateFile', 'checkExtensionByMimeType' => false]
['attr_image', 'file', 'extensions' => ['png'], 'on' => 'validateFile', 'checkExtensionByMimeType' => false],
['!safe_attr', 'integer']
];
}

Expand Down
14 changes: 14 additions & 0 deletions tests/framework/validators/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,18 @@ public function testAddError()
$errors = $m->getErrors('attr_msg_val');
$this->assertEquals('attr_msg_val::abc::param_value', $errors[0]);
}

public function testGetActiveValidatorsForSafeAttributes()
{
$model = $this->getTestModel();
$validators = $model->getActiveValidators('safe_attr');
$is_found = false;
foreach ($validators as $v) {
if ($v instanceof NumberValidator) {
$is_found = true;
break;
}
}
$this->assertTrue($is_found);
}
}

0 comments on commit 2de18cf

Please sign in to comment.