mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
* `yii\validators\DateValidator` now resets `$timestampAttribute` value on empty validated attribute value * array-value test at `DateValidatorTest` restored
This commit is contained in:

committed by
Carsten Brandt

parent
b8d5a35916
commit
40e5702b6b
@ -104,6 +104,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #14059: Removed unused AR instantiating for calling of static methods (ElisDN)
|
- 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 #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 #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 #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)
|
- 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)
|
- 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)
|
||||||
|
@ -266,6 +266,13 @@ class DateValidator extends Validator
|
|||||||
public function validateAttribute($model, $attribute)
|
public function validateAttribute($model, $attribute)
|
||||||
{
|
{
|
||||||
$value = $model->$attribute;
|
$value = $model->$attribute;
|
||||||
|
if ($this->isEmpty($value)) {
|
||||||
|
if ($this->timestampAttribute !== null) {
|
||||||
|
$model->{$this->timestampAttribute} = null;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$timestamp = $this->parseDateValue($value);
|
$timestamp = $this->parseDateValue($value);
|
||||||
if ($timestamp === false) {
|
if ($timestamp === false) {
|
||||||
if ($this->timestampAttribute === $attribute) {
|
if ($this->timestampAttribute === $attribute) {
|
||||||
|
@ -155,11 +155,11 @@ class DateValidatorTest extends TestCase
|
|||||||
1379030400, // 2013-09-13 00:00:00
|
1379030400, // 2013-09-13 00:00:00
|
||||||
$model->attr_timestamp
|
$model->attr_timestamp
|
||||||
);
|
);
|
||||||
|
// array value
|
||||||
$val = new DateValidator(['format' => 'php:Y-m-d']);
|
$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');
|
$val->validateAttribute($model, 'attr_date');
|
||||||
$this->assertTrue($model->hasErrors('attr_date'));
|
$this->assertTrue($model->hasErrors('attr_date'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -199,10 +199,12 @@ class DateValidatorTest extends TestCase
|
|||||||
1379030400, // 2013-09-13 00:00:00
|
1379030400, // 2013-09-13 00:00:00
|
||||||
$model->attr_timestamp
|
$model->attr_timestamp
|
||||||
);
|
);
|
||||||
|
// array value
|
||||||
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
|
$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');
|
$val->validateAttribute($model, 'attr_date');
|
||||||
$this->assertTrue($model->hasErrors('attr_date'));
|
$this->assertTrue($model->hasErrors('attr_date'));
|
||||||
|
// invalid format
|
||||||
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
|
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
|
||||||
$model = FakedValidationModel::createWithAttributes(['attr_date' => '2012-12-12foo']);
|
$model = FakedValidationModel::createWithAttributes(['attr_date' => '2012-12-12foo']);
|
||||||
$val->validateAttribute($model, 'attr_date');
|
$val->validateAttribute($model, 'attr_date');
|
||||||
@ -601,4 +603,33 @@ class DateValidatorTest extends TestCase
|
|||||||
$val->validateAttribute($model, 'attr_date');
|
$val->validateAttribute($model, 'attr_date');
|
||||||
$this->assertTrue($model->hasErrors('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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user