From 35c11f3d78d6c2a619a9f68fff0dcfd3da6b5489 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 19 Feb 2015 17:38:12 +0300 Subject: [PATCH] Updated config and added date time attribute --- DatePickerConfig.php | 10 +++++++--- DateTimeAttribute.php | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/DatePickerConfig.php b/DatePickerConfig.php index 58a9a29..6ad91ab 100644 --- a/DatePickerConfig.php +++ b/DatePickerConfig.php @@ -2,10 +2,8 @@ namespace omnilight\datetime; -use yii\base\InvalidParamException; use yii\helpers\ArrayHelper; use yii\helpers\FormatConverter; -use yii\i18n\Formatter; /** @@ -27,10 +25,16 @@ public static function get(DateTimeAttribute $attribute, $options = [], $datePic $defaults = [ 'language' => \Yii::$app->language, 'clientOptions' => [ - 'dateFormat' => 'php://'.FormatConverter::convertDateIcuToJui($format[1], $format[0]), + 'dateFormat' => 'php:' . FormatConverter::convertDateIcuToJui($format[1], $format[0]), ] ]; break; + case 'omnilight\widgets\DatePicker': + $defaults = [ + 'language' => \Yii::$app->language, + 'dateFormat' => 'php:' . FormatConverter::convertDateIcuToPhp($format[1], $format[0]), + ]; + break; default: return $options; } diff --git a/DateTimeAttribute.php b/DateTimeAttribute.php index 85f80b3..95e95d6 100644 --- a/DateTimeAttribute.php +++ b/DateTimeAttribute.php @@ -3,6 +3,7 @@ namespace omnilight\datetime; use yii\base\Object; +use yii\helpers\FormatConverter; /** @@ -67,6 +68,9 @@ public function getValue() public function setValue($value) { $this->_value = $value; - $this->behavior->owner->{$this->originalAttribute} = $this->behavior->formatter->format($this->_value, $this->originalFormat); + $normalizedFormat = DateTimeBehavior::normalizeIcuFormat($this->targetFormat, $this->behavior->formatter); + $phpFormat = FormatConverter::convertDateIcuToPhp($normalizedFormat[1], $normalizedFormat[0], \Yii::$app->language); + $value = date_create_from_format($phpFormat, $value); + $this->behavior->owner->{$this->originalAttribute} = $this->behavior->formatter->format($value, $this->originalFormat); } } \ No newline at end of file