Fixes #5108 DateValidator resets $timestampAttribute value on empty attribute (#14242)

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

* array-value test at `DateValidatorTest` restored
This commit is contained in:
Paul Klimov
2017-06-05 12:50:02 +03:00
committed by Carsten Brandt
parent b8d5a35916
commit 40e5702b6b
3 changed files with 42 additions and 3 deletions

View File

@ -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)

View File

@ -266,6 +266,13 @@ class DateValidator extends Validator
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) {

View File

@ -155,11 +155,11 @@ class DateValidatorTest extends TestCase
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'));
}
/**
@ -199,10 +199,12 @@ class DateValidatorTest extends TestCase
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');
@ -601,4 +603,33 @@ class DateValidatorTest extends TestCase
$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);
}
}