Browse Source

Make 'safe' validator work on write-only properties

fixes #12605
tags/2.0.10
Carsten Brandt 8 years ago
parent
commit
89bc2918d3
  1. 3
      framework/CHANGELOG.md
  2. 7
      framework/validators/SafeValidator.php
  3. 31
      tests/framework/base/ModelTest.php

3
framework/CHANGELOG.md

@ -44,9 +44,10 @@ Yii Framework 2 Change Log
- Bug #12537: Fixes issues with spaces in `StringHelper:truncateHtml` (Alex-Code)
- Bug #12554: Fixed `yii\validators\UniqueValidator` error of getting first model indexed by field (DrDeath72)
- Bug #12599: Fixed casting of `binary()` type for MSSQL (silverfire)
- Bug #12605: Make 'safe' validator work on write-only properties (arthibald, CeBe)
- Bug #12629: Fixed `yii\widgets\ActiveField::widget()` to call `adjustLabelFor()` for `InputWidget` descendants (coderlex)
- Bug #12649: Fixed consistency of `indexBy` handling for `yii\db\Query::column()` (silverfire)
- Enh #384: Added ability to run migration from several locations via `yii\console\controllers\BaseMigrateController::migrationNamespaces` (klimov-paul)
- Enh #384: Added ability to run migration from several locations via `yii\console\controllers\BaseMigrateController::$migrationNamespaces` (klimov-paul)
- Enh #6996: Added `yii\web\MultipartFormDataParser`, which allows proper processing of 'multipart/form-data' encoded non POST requests (klimov-paul)
- Enh #8719: Add support for HTML5 attributes on submitbutton (formaction/formmethod...) for ActiveForm (VirtualRJ)
- Enh #9469: Added support for namespaced migrations via `yii\console\controllers\BaseMigrateController::migrationNamespaces` (klimov-paul)

7
framework/validators/SafeValidator.php

@ -26,6 +26,13 @@ class SafeValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttributes($model, $attributes = null)
{
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
}

31
tests/framework/base/ModelTest.php

@ -398,6 +398,20 @@ class ModelTest extends TestCase
$invalid = new InvalidRulesModel();
$invalid->createValidators();
}
/**
* Ensure 'safe' validator works for write-only properties.
* Normal validator can not work here though.
*/
public function testValidateWriteOnly()
{
$model = new WriteOnlyModel();
$model->setAttributes(['password' => 'test'], true);
$this->assertEquals('test', $model->passwordHash);
$this->assertTrue($model->validate());
}
}
class ComplexModel1 extends Model
@ -423,3 +437,20 @@ class ComplexModel2 extends Model
];
}
}
class WriteOnlyModel extends Model
{
public $passwordHash;
public function rules()
{
return [
[['password'],'safe'],
];
}
public function setPassword($pw)
{
$this->passwordHash = $pw;
}
}
Loading…
Cancel
Save