Commit 39c796e4 by Carsten Brandt

chanded jui DatePicker widget to use ICU date format

- use default application formatting for the date value - auto adjust to application language and format setting changes - be consistent with formatter and date validator fixes #1551
parent aa8c013e
......@@ -4,7 +4,10 @@ Yii Framework 2 jui extension Change Log
2.0.0-rc under development
--------------------------
- no changes in this release.
- Chg #1551: Jui datepicker has a new property `$dateFormat` which is used to set the clientOption `dateFormat`.
The new property does not use the datepicker formatting synax anymore but uses the same as the `yii\i18n\Formatter`
class which is the ICU syntax for date formatting, you have to adjust all your DatePicker widgets to use
the new property instead of setting the dateFormat in the clientOptions (cebe)
2.0.0-beta April 13, 2014
......
......@@ -8,22 +8,21 @@
namespace yii\jui;
use Yii;
use yii\helpers\FormatConverter;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* DatePicker renders a datepicker jQuery UI widget.
* DatePicker renders a `datepicker` jQuery UI widget.
*
* For example:
* For example to use the datepicker with a [[yii\base\Model|model]]:
*
* ```php
* echo DatePicker::widget([
* 'language' => 'ru',
* 'model' => $model,
* 'attribute' => 'from_date',
* 'clientOptions' => [
* 'dateFormat' => 'yy-mm-dd',
* ],
* //'language' => 'ru',
* //'dateFormat' => 'yyyy-MM-dd',
* ]);
* ```
*
......@@ -31,16 +30,16 @@ use yii\helpers\Json;
*
* ```php
* echo DatePicker::widget([
* 'language' => 'ru',
* 'name' => 'from_date',
* 'clientOptions' => [
* 'dateFormat' => 'yy-mm-dd',
* ],
* 'value' => $value,
* //'language' => 'ru',
* //'dateFormat' => 'yyyy-MM-dd',
* ]);
* ```
*
* @see http://api.jqueryui.com/datepicker/
* @author Alexander Kochetov <creocoder@gmail.com>
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class DatePicker extends InputWidget
......@@ -59,6 +58,37 @@ class DatePicker extends InputWidget
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $containerOptions = [];
/**
* @var string the format string to be used for formatting the date value. This option will be used
* to populate the [[clientOptions|clientOption]] `dateFormat`.
* The value can be one of "short", "medium", "long", or "full", which represents a preset format of different lengths.
*
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax).
* Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the
* PHP [date()](http://php.net/manual/de/function.date.php)-function.
*
* For example:
*
* ```php
* 'MM/dd/yyyy' // date in ICU format
* 'php:m/d/Y' // the same date in PHP format
* ```
*
* If not set the default value will be taken from `Yii::$app->formatter->dateFormat`.
*/
public $dateFormat;
/**
* @var string the model attribute that this widget is associated with.
* The value of the attribute will be converted using [[\yii\i18n\Formatter::asDate()|`Yii::$app->formatter->asDate()`]]
* with the [[dateFormat]] if it is not null.
*/
public $attribute;
/**
* @var string the input value.
* This value will be converted using [[\yii\i18n\Formatter::asDate()|`Yii::$app->formatter->asDate()`]]
* with the [[dateFormat]] if it is not null.
*/
public $value;
/**
......@@ -70,6 +100,9 @@ class DatePicker extends InputWidget
if ($this->inline && !isset($this->containerOptions['id'])) {
$this->containerOptions['id'] = $this->options['id'] . '-container';
}
if ($this->dateFormat === null) {
$this->dateFormat = Yii::$app->formatter->dateFormat;
}
}
/**
......@@ -82,6 +115,12 @@ class DatePicker extends InputWidget
$containerID = $this->inline ? $this->containerOptions['id'] : $this->options['id'];
$language = $this->language ? $this->language : Yii::$app->language;
if (strncmp($this->dateFormat, 'php:', 4) === 0) {
$this->clientOptions['dateFormat'] = FormatConverter::convertDatePhpToJui(substr($this->dateFormat, 4), 'date', $language);
} else {
$this->clientOptions['dateFormat'] = FormatConverter::convertDateIcuToJui($this->dateFormat, 'date', $language);
}
if ($language != 'en-US') {
$view = $this->getView();
$bundle = DatePickerLanguageAsset::register($view);
......@@ -108,20 +147,33 @@ class DatePicker extends InputWidget
{
$contents = [];
// get formatted date value
if ($this->hasModel()) {
$value = Html::getAttributeValue($this->model, $this->attribute);
} else {
$value = $this->value;
}
if ($value !== null) {
$value = Yii::$app->formatter->asDate($value, $this->dateFormat);
}
$options = $this->options;
$options['value'] = $value;
if ($this->inline === false) {
// render a text input
if ($this->hasModel()) {
$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
$contents[] = Html::activeTextInput($this->model, $this->attribute, $options);
} else {
$contents[] = Html::textInput($this->name, $this->value, $this->options);
$contents[] = Html::textInput($this->name, $value, $options);
}
} else {
// render an inline date picker with hidden input
if ($this->hasModel()) {
$contents[] = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
$this->clientOptions['defaultDate'] = $this->model->{$this->attribute};
$contents[] = Html::activeHiddenInput($this->model, $this->attribute, $options);
} else {
$contents[] = Html::hiddenInput($this->name, $this->value, $this->options);
$this->clientOptions['defaultDate'] = $this->value;
$contents[] = Html::hiddenInput($this->name, $value, $options);
}
$this->clientOptions['defaultDate'] = $value;
$this->clientOptions['altField'] = '#' . $this->options['id'];
$contents[] = Html::tag('div', null, $this->containerOptions);
}
......
JUI Extension for Yii 2
=======================
This is the JQuery UI extension for Yii 2. It encapsulates JQuery UI widgets as Yii widgets,
This is the JQuery UI extension for Yii 2. It encapsulates [JQuery UI widgets](http://jqueryui.com/) as Yii widgets,
and makes using JQuery UI widgets in Yii applications extremely easy. For example, the following
single line of code in a view file would render a JQuery UI DatePicker widget:
single line of code in a view file would render a [JQuery UI DatePicker](http://api.jqueryui.com/datepicker/) widget:
```php
<?= yii\jui\DatePicker::widget(['name' => 'attributeName']) ?>
```
Configuring the Jquery UI options should be done using the clientOptions attribute:
```php
<?= yii\jui\DatePicker::widget(['name' => 'attributeName', 'clientOptions' => ['dateFormat' => 'yy-mm-dd']]) ?>
<?= yii\jui\DatePicker::widget(['name' => 'attributeName', 'clientOptions' => ['defaultDate' => '2014-01-01']]) ?>
```
If you want to use the JUI widget in an ActiveRecord form, it can be done like this:
If you want to use the JUI widget in an ActiveForm, it can be done like this:
```php
<?= $form->field($model,'attributeName')->widget(DatePicker::className(),['clientOptions' => ['dateFormat' => 'yy-mm-dd']]) ?>
<?= $form->field($model,'attributeName')->widget(DatePicker::className(),['clientOptions' => ['defaultDate' => '2014-01-01']]) ?>
```
......@@ -38,4 +40,3 @@ or add
```
to the require section of your `composer.json` file.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment