Skip to content

Commit

Permalink
Fixes yiisoft#5108 DateValidator resets $timestampAttribute value…
Browse files Browse the repository at this point in the history
… on empty attribute (yiisoft#14242)

* `yii\validators\DateValidator` now resets `$timestampAttribute` value on empty validated attribute value

* array-value test at `DateValidatorTest` restored
  • Loading branch information
klimov-paul authored and cebe committed Jun 5, 2017
1 parent b8d5a35 commit 40e5702
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Yii Framework 2 Change Log
- Enh #14059: Removed unused AR instantiating for calling of static methods (ElisDN)
- Enh #14067: `yii\web\View::clear()` sets populated arrays to empty arrays instead of null, also changed default values to empty array (craiglondon)
- Enh #4793: `yii\filters\AccessControl` now can be used without `user` component (bizley)
- Enh #5108: `yii\validators\DateValidator` now resets `$timestampAttribute` value on empty validated attribute value (klimov-paul)
- Enh #4999: Added support for wildcards at `yii\filters\AccessRule::$controllers` (klimov-paul)
- Enh: Added `yii\di\Instance::__set_state()` method to restore object after serialization using `var_export()` function (silvefire)
- Bug #14072: Fixed a bug where `\yii\db\Command::createTable()`, `addForeignKey()`, `dropForeignKey()`, `addCommentOnColumn()`, and `dropCommentFromColumn()` weren't refreshing the table cache on `yii\db\Schema` (brandonkelly)
Expand Down
7 changes: 7 additions & 0 deletions framework/validators/DateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ public function init()
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
if ($this->isEmpty($value)) {
if ($this->timestampAttribute !== null) {
$model->{$this->timestampAttribute} = null;
}
return;
}

$timestamp = $this->parseDateValue($value);
if ($timestamp === false) {
if ($this->timestampAttribute === $attribute) {
Expand Down
37 changes: 34 additions & 3 deletions tests/framework/validators/DateValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public function testValidateAttributePHPFormat($timezone)
1379030400, // 2013-09-13 00:00:00
$model->attr_timestamp
);
// array value
$val = new DateValidator(['format' => 'php:Y-m-d']);
$model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
$model = FakedValidationModel::createWithAttributes(['attr_date' => ['2013-09-13']]);
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));

}

/**
Expand Down Expand Up @@ -199,10 +199,12 @@ public function testValidateAttributeICUFormat($timezone)
1379030400, // 2013-09-13 00:00:00
$model->attr_timestamp
);
// array value
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
$model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
$model = FakedValidationModel::createWithAttributes(['attr_date' => ['2013-09-13']]);
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));
// invalid format
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
$model = FakedValidationModel::createWithAttributes(['attr_date' => '2012-12-12foo']);
$val->validateAttribute($model, 'attr_date');
Expand Down Expand Up @@ -601,4 +603,33 @@ public function testTimestampAttributeSkipValidation()
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));
}

/**
* @depends testValidateAttributePHPFormat
*/
public function testTimestampAttributeOnEmpty()
{
$validator = new DateValidator([
'format' => 'php:Y/m/d',
'timestampAttribute' => 'attr_date',
'skipOnEmpty' => false,
]);
$model = new FakedValidationModel();
$model->attr_date = '';
$validator->validateAttribute($model, 'attr_date');
$this->assertFalse($model->hasErrors('attr_date'));
$this->assertNull($model->attr_date);

$validator = new DateValidator([
'format' => 'php:Y/m/d',
'timestampAttribute' => 'attr_timestamp',
'skipOnEmpty' => false,
]);
$model = new FakedValidationModel();
$model->attr_date = '';
$model->attr_timestamp = 1379030400;
$validator->validateAttribute($model, 'attr_date');
$this->assertFalse($model->hasErrors('attr_date'));
$this->assertNull($model->attr_timestamp);
}
}

0 comments on commit 40e5702

Please sign in to comment.