From 89bc2918d3f56a9642c9860311e77cb0271a8f36 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 6 Oct 2016 14:41:13 +0200 Subject: [PATCH] Make 'safe' validator work on write-only properties fixes #12605 --- framework/CHANGELOG.md | 3 ++- framework/validators/SafeValidator.php | 7 ++++++ tests/framework/base/ModelTest.php | 31 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 1b6d313f64..ff983c6c1d 100644 --- a/framework/CHANGELOG.md +++ b/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) diff --git a/framework/validators/SafeValidator.php b/framework/validators/SafeValidator.php index ec0252d340..a43f79dc95 100644 --- a/framework/validators/SafeValidator.php +++ b/framework/validators/SafeValidator.php @@ -23,6 +23,13 @@ namespace yii\validators; */ class SafeValidator extends Validator { + /** + * @inheritdoc + */ + public function validateAttributes($model, $attributes = null) + { + } + /** * @inheritdoc */ diff --git a/tests/framework/base/ModelTest.php b/tests/framework/base/ModelTest.php index 2bf404268d..fd47447b7c 100644 --- a/tests/framework/base/ModelTest.php +++ b/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; + } +} \ No newline at end of file