Skip to content

Commit

Permalink
Fixes yiisoft#5108: Cleaning timestampAttribute when skipOnEmpty is s…
Browse files Browse the repository at this point in the history
…et to false
  • Loading branch information
Гонимар Сергей committed Dec 15, 2016
1 parent b623497 commit f569061
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions framework/validators/DateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]);
Expand Down
23 changes: 23 additions & 0 deletions tests/framework/validators/DateValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f569061

Please sign in to comment.