Browse Source

Fixed DateValidator to respect time when `format` is `php:U`

Fixes #15628
tags/2.0.14
SilverFire - Dmitry Naumenko 7 years ago
parent
commit
a4b6b853b6
No known key found for this signature in database
GPG Key ID: 39DD917A92B270A
  1. 2
      framework/CHANGELOG.md
  2. 2
      framework/validators/DateValidator.php
  3. 16
      tests/framework/validators/DateValidatorTest.php

2
framework/CHANGELOG.md

@ -5,6 +5,8 @@ Yii Framework 2 Change Log
------------------------
- Bug #15658: Fixed `yii\filters\auth\HttpBasicAuth` not to switch identity, when user is already authenticated and identity does not get changed (silverfire)
- Bug #15628: Fixed `yii\validators\DateValidator` to respect time when the `format` property is set to UNIX Epoch format (silverfire, gayHacker)
- Bug #15658: Fixed `yii\db\HttpBasicAuth` not to switch identity, when user is already authenticated and identity does not get changed (silverfire)
- Bug #9342: Fixed `yii\db\ActiveQueryTrait` to apply `indexBy` after relations population in order to prevent excess queries (sammousa, silverfire)
- Enh #13425: Added caching of dynamically added URL rules with `yii\web\UrlManager::addRules()` (scriptcube, silverfire)
- Bug #15644: Avoid wrong default selection on a dropdown, checkbox list, and radio list, when a option has a key equals to zero (berosoboy)

2
framework/validators/DateValidator.php

@ -402,7 +402,7 @@ class DateValidator extends Validator
private function parseDateValuePHP($value, $format)
{
// if no time was provided in the format string set time to 0 to get a simple date timestamp
$hasTimeInfo = (strpbrk($format, 'HhGgis') !== false);
$hasTimeInfo = (strpbrk($format, 'HhGgisU') !== false);
$date = DateTime::createFromFormat($format, $value, new \DateTimeZone($hasTimeInfo ? $this->timeZone : 'UTC'));
$errors = DateTime::getLastErrors();

16
tests/framework/validators/DateValidatorTest.php

@ -675,4 +675,20 @@ class DateValidatorTest extends TestCase
$this->assertFalse($model->hasErrors('attr_date'));
$this->assertNull($model->attr_timestamp);
}
/**
* Tests that DateValidator with format `php:U` does not truncate timestamp to date.
* @see https://github.com/yiisoft/yii2/issues/15628
*/
public function testIssue15628()
{
$validator = new DateValidator(['format' => 'php:U' , 'type' => DateValidator::TYPE_DATETIME, 'timestampAttribute' => 'attr_date']);
$model = new FakedValidationModel();
$value = 1518023610;
$model->attr_date = $value;
$validator->validateAttribute($model, 'attr_date');
$this->assertEquals($value, $model->attr_date);
}
}

Loading…
Cancel
Save