public function testValidateAttribute() { // error-array-add $val = new DateValidator(); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $model = new FakedValidationModel(); $model->attr_date = '1375293913'; $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); //// timestamp attribute $val = new DateValidator(['timestampAttribute' => 'attr_timestamp']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertEquals(DateTime::createFromFormat($val->format, '2013-09-13')->getTimestamp(), $model->attr_timestamp); $val = new DateValidator(); $model = FakedValidationModel::createWithAttributes(['attr_date' => []]); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); }
/** * * @param type $model * @param type $attribute */ public function validateAttribute($model, $attribute) { parent::validateAttribute($model, $attribute); $errors = $model->getErrors($attribute); $value = $model->{$attribute}; if (empty($errors) and !empty($value)) { $value = \DateTime::createFromFormat('d.m.Y', $value); $model->{$attribute} = $value; } }
/** * @inheritdoc */ public function validateAttribute($model, $attribute) { $mongoDateAttribute = $this->mongoDateAttribute; if ($this->timestampAttribute === null) { $this->timestampAttribute = $mongoDateAttribute; } $originalErrorCount = count($model->getErrors($attribute)); parent::validateAttribute($model, $attribute); $afterValidateErrorCount = count($model->getErrors($attribute)); if ($originalErrorCount === $afterValidateErrorCount) { if ($this->mongoDateAttribute !== null) { $timestamp = $model->{$this->timestampAttribute}; $mongoDateAttributeValue = $model->{$this->mongoDateAttribute}; // ensure "dirty attributes" support : if (!$mongoDateAttributeValue instanceof \MongoDate || $mongoDateAttributeValue->sec !== $timestamp) { $model->{$this->mongoDateAttribute} = new \MongoDate($timestamp); } } } }
/** * @inheritdoc */ public function validateAttribute($model, $attribute) { $mongoDateAttribute = $this->mongoDateAttribute; if ($this->timestampAttribute === null) { $this->timestampAttribute = $mongoDateAttribute; } $originalErrorCount = count($model->getErrors($attribute)); parent::validateAttribute($model, $attribute); $afterValidateErrorCount = count($model->getErrors($attribute)); if ($originalErrorCount === $afterValidateErrorCount) { if ($this->mongoDateAttribute !== null) { $timestamp = $model->{$this->timestampAttribute}; $mongoDateAttributeValue = $model->{$this->mongoDateAttribute}; // ensure "dirty attributes" support : if (!$mongoDateAttributeValue instanceof \MongoDB\BSON\UTCDatetime || $mongoDateAttributeValue->toDateTime()->format('U') !== $timestamp) { // Must be 64bit integer $milliseconds = (string) $timestamp . '000'; $model->{$this->mongoDateAttribute} = new \MongoDB\BSON\UTCDatetime($milliseconds); } } } }
public function testValidateAttributeICUFormat() { // error-array-add $val = new DateValidator(['format' => 'yyyy-MM-dd']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $model = new FakedValidationModel(); $model->attr_date = '1375293913'; $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); //// timestamp attribute $val = new DateValidator(['format' => 'yyyy-MM-dd', 'timestampAttribute' => 'attr_timestamp']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertEquals(mktime(0, 0, 0, 9, 13, 2013), $model->attr_timestamp); $val = new DateValidator(['format' => 'yyyy-MM-dd']); $model = FakedValidationModel::createWithAttributes(['attr_date' => []]); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); }
/** * @dataProvider provideTimezones */ public function testValidationWithTimeAndOutputTimeZone($timezone) { date_default_timezone_set($timezone); $val = new DateValidator(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'UTC']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13 14:23:15'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); $val = new DateValidator(['format' => 'php:Y-m-d H:i:s', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'UTC']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13 14:23:15'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); $val = new DateValidator(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'Europe/Berlin']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13 16:23:15'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); $val = new DateValidator(['format' => 'php:Y-m-d H:i:s', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'Europe/Berlin']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13 16:23:15'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); $val = new DateValidator(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'America/New_York', 'timeZone' => 'Europe/Berlin']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13 16:23:15'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertSame('2013-09-13 10:23:15', $model->attr_timestamp); $val = new DateValidator(['format' => 'php:Y-m-d H:i:s', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'America/New_York', 'timeZone' => 'Europe/Berlin']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13 16:23:15'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertSame('2013-09-13 10:23:15', $model->attr_timestamp); }
public function testIntlMultibyteString() { $val = new DateValidator(['format' => 'dd MMM yyyy', 'locale' => 'de_DE']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 Mai 2014']); $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $val = new DateValidator(['format' => 'dd MMM yyyy', 'locale' => 'ru_RU']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 мая 2014']); $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); }