From f569061eb2f8b15767b6a60e0a36acddbdbc4001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BD=D0=B8=D0=BC=D0=B0=D1=80=20=D0=A1=D0=B5?= =?UTF-8?q?=D1=80=D0=B3=D0=B5=D0=B9?= Date: Thu, 15 Dec 2016 14:51:29 +0500 Subject: [PATCH] Fixes #5108: Cleaning timestampAttribute when skipOnEmpty is set to false --- framework/validators/DateValidator.php | 11 ++++----- .../validators/DateValidatorTest.php | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/framework/validators/DateValidator.php b/framework/validators/DateValidator.php index ce558118e4b..2478063858b 100644 --- a/framework/validators/DateValidator.php +++ b/framework/validators/DateValidator.php @@ -266,12 +266,6 @@ public function init() public function validateAttribute($model, $attribute) { $value = $model->$attribute; - - if ($this->skipOnEmpty === false && empty($value) && $this->timestampAttribute !== null) { - $model->{$this->timestampAttribute} = null; - return; - } - $timestamp = $this->parseDateValue($value); if ($timestamp === false) { if ($this->timestampAttribute === $attribute) { @@ -285,6 +279,11 @@ public function validateAttribute($model, $attribute) } } } + // skipOnEmpty is false, clear the timestamp attribute + if (empty($value) && $this->timestampAttribute !== null) { + $model->{$this->timestampAttribute} = null; + return; + } $this->addError($model, $attribute, $this->message, []); } elseif ($this->min !== null && $timestamp < $this->min) { $this->addError($model, $attribute, $this->tooSmall, ['min' => $this->minString]); diff --git a/tests/framework/validators/DateValidatorTest.php b/tests/framework/validators/DateValidatorTest.php index 99740d1f802..74c0efd0022 100644 --- a/tests/framework/validators/DateValidatorTest.php +++ b/tests/framework/validators/DateValidatorTest.php @@ -616,5 +616,28 @@ public function testTimestampAttributeEmptyValueValidation() $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertNull($model->attr_timestamp); + + $val = new DateValidator(['format' => 'php:Y-m-d', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'skipOnEmpty' => false]); + $model = new FakedValidationModel; + $model->attr_date = ''; + $model->attr_timestamp = 1379030400; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertNull($model->attr_timestamp); + + $val = new DateValidator(['format' => 'php:Y/m/d', 'timestampAttribute' => 'attr_date', 'skipOnEmpty' => false]); + $model = new FakedValidationModel(); + $model->attr_date = ''; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertNull($model->attr_date); + + $val = new DateValidator(['format' => 'php:Y/m/d', 'timestampAttribute' => 'attr_date', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'skipOnEmpty' => false]); + $model = new FakedValidationModel(); + $model->attr_date = ''; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertNull($model->attr_date); } }