Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatetimePicker no inital value and other position #96

Closed
Enrica-r opened this issue Jun 24, 2014 · 14 comments
Closed

DatetimePicker no inital value and other position #96

Enrica-r opened this issue Jun 24, 2014 · 14 comments

Comments

@Enrica-r
Copy link

I have one page in which I use DatePicker (via DateControl) and DatetimePicker (also via DateControl)
DatetimePicker starts with a date in year 1899 always while Datepicker takes the given value from input. see Screenshots:
datetimepicker_nok
datepicker_ok

Further Datepicker checks which is best position top or bottom and it starts left of input field. Datetimepicker goes out of screen without pluginOptions. In screenshot I set 'pluginOptions' => ['pickerPosition' => 'top-left']. As you can see it's better but it's not at the same place as Datepicker. It's not relevant for functionality but it's not good for application's common behavior.

@kartik-v
Copy link
Owner

  • Firstly, I hope you are upgraded to the latest code. Recently the widget is also updated to the latest bootstrap-datetimepicker plugin.
  • Next, regarding first problem of wrong year in date time picker - it seems like there is some mismatch in the date format and the value that you have passed to the input (and its converting wrongly). Ensure that format for the model attribute value and the saveFormat match. Else, debug/post your findings on the value passed and formats.
  • Thirdly on the positioning of the picker - its based on the external source plugin feature (which is a bit different for both). Can try raising a PR for the malot datetime picker. See related issue 95 and 209. If you want consistent behavior - I would recommend you to use the picker icon to the left. For example the following will work similarly for both:
<?= $form->field($model, 'from_date')->widget(DateControl::classname(),[
    'type' => DateControl::FORMAT_DATE,
]); ?>
<?= $form->field($model, 'datetime_1')->widget(DateControl::classname(), [
    'type'=>DateControl::FORMAT_DATETIME,
    'options' => [
        'pluginOptions' => [
            'pickerPosition' => 'bottom-right'
        ]
    ]
]); ?>

@Enrica-r
Copy link
Author

@kartik-v Hello Kartik
First: Yes it's the current code updated yesterday.
Second: I debuged all and I found out that's a bug is in DateControl. I configured Datecontrol on level yii config with "autoWidget = false".

As you can see in the following picture in method getDisplayInput() the value of $this->displayFormat is correct formatted same as value. But $this->displayFormat will never passed to $this->options because ArrayHelper:: merge... with Module::defaultWidgetOptionshas never been passed. Therefore option convertFormatisn't set and pluginOption formatisn't set. It's obvious that the following widget will fail.
datecontrol_error

My next question was: "why does DatetimePicker fail while DatePicker works?"
DatePicker and DateTimepicker have localized default settings in bootstrap-datetimepicker.de.js. In DatePicker the entry is correct while DatetimePicker has defined format: "dd.mm.yyyy" instead of format: "dd.mm.yyyy hh:ii:ss". see:
locale_options_default

@kartik-v
Copy link
Owner

@erik-r With regards to the datetimepicker plugin... its better if a PR is raised on this with the author. I have currently updated the locale js files but its better if its done in the source plugin for future updates.

Will check on the other details that you have mentioned.

@kartik-v
Copy link
Owner

@erik-r the Module::defaultWidgetOptions will only be used if autoWidget is true (which means the default setting using \kartik\widgets`).

If autoWidget is set to false it means your own manual configuration. Since this is what you have in your case, you need to setup your widget settings in the module yourself. So you must setup your default pluginOptions like: convertFormat to true and/or setup the format, showSeconds, etc.

@Enrica-r
Copy link
Author

@kartik-v Concerning formatting I'm not your opinion with following arguments:

  • Date format pattern must be always synchronized. If initial value is formatted per example like 31.12.2014 (php: d.m.Y) in DateControl it must the same in DatePicker ALWAYS. DatePicker must have dd.mm.yyyyin this example. It make absolutely no sense to have mm/dd/yyyy in DatePicker. DateControl as central plattform should ensure the synchronization of formats definitions.
  • In yii config file developer can't call yii::$app->formatter->dateFormat because yii object isn't loaded at this time.
  • DateControl does format initial value already with a given format pattern. Therefore DateControl should send the format pattern information to sub extension also (in minimum to own kartik's DatePicker).
  • To keep the usage of DateControl easy and robust display format options should be take from two sources only: a) config displaySetting in widget call or module config or b) if not configured then from yii::$app->formatter. If your widget DatePicker is used then I even wouldn't give the option to set pluginOption 'format' and option 'convertFormat'. I would set 'format' with 'displayFormat' and set 'convertFormat' = true fix (override a developers definition).
  • If a developer want's to use Yii-Jui DatePicker then he has to convert PHP format pattern to Jui format. Unfortunately DateControl must accept a pluginOption 'format'. But developer must write his own conversion function anyway.
  • Config entries in 'widgetSettings' should give the possibilities to control the behavior of DatePicker like 'prepend', 'append', 'language' etc. but not format option (see above).

Without a change on your side a developer must inherit your class and override 'init' method if he wants to have dynamically programed localized application. Thank you for your good work again.

@kartik-v
Copy link
Owner

@erik-r - DateControl already formats and converts date to javascript format... for known widget types - kartik\widgets\DatePicker and kartik\widgets\DateTimePicker (when you set autoWidget to true). These widgets have been enhanced to understand and convert the format.

But it cannot do it for other arbitrary third party widgets or plugins - because it depends on the way plugin is coded and the plugin may have its own property naming and options to control formats.

Hence, in such cases (when autoWidget is set to false), ability is given to developer to setup widget & plugin options. All of this can be setup from the module configuration - without writing any code to extend the DateControl class.

Are you facing a challenge setting your own custom defaults for the widget from the module? The displayFormat you need can be setup within the module configuration which can be picked up by DatePicker.

@Enrica-r
Copy link
Author

@kartik-v Yes I know that DateControl can't set pluginOptions for other plugins. In this case I agree with you completely. But for your own DatePicker and Datetimepicker also you can. autoWidget is OK but in this case I can't configure the behavior of your own DatePicker on one place.

I'm thinking about working with yii::$app->params in which I could set behaviors 'optionPlugin' for your DatePicker. Then I will set autoWidget = true and when calling DateControl widget I will pass params. If this works I don't use module settings.

I will try the same with your activeForm settings.

@kartik-v
Copy link
Owner

@erik-r you probably can achieve this easier than that. You can use Yii's feature to set global class defaults this way. For example, you can call this in your config or index file:

\Yii::$container->set('kartik\widgets\DatePicker', [
    'pluginOptions' => [
        'format' => 'dd.mm.yyyy',
    ],
]);

I am not aware of your configuration. But I think you SHOULD BE ABLE TO setup the DateControl module defaults this way (and setup your widget format defaults for your language):

use kartik\datecontrol\Module;
...
'modules' => [
    'datecontrol' => [
        'class' => 'kartik\datecontrol\Module',

        // format settings for displaying each date attribute
        'displaySettings' => [
            Module::FORMAT_DATE => 'd.m.Y',
            Module::FORMAT_TIME => 'H:i:s',
            Module::FORMAT_DATETIME => 'd.m.Y H:i:s',
        ],

        // format settings for saving each date attribute
        'saveSettings' => [
            Module::FORMAT_DATE => 'Y-m-d', 
            Module::FORMAT_TIME => 'H:i:s',
            Module::FORMAT_DATETIME => 'Y-m-d H:i:s'
        ],

        // disable automatic use of kartik\widgets for each of the above formats
        'autoWidget' => false,

        // setup your date picker widget default options
        'widgetSettings' => [
            Module::FORMAT_DATE => [
                'class' => 'kartik\widgets\DatePicker',
                'options' => [
                    'convertFormat'=>true,
                    'pluginOptions'=>['format'=>'dd.mm.yyyy'],
                ]
            ],
        ],

    // other settings
],

@Enrica-r
Copy link
Author

Thank your for info. It's work perfectly.

@Enrica-r
Copy link
Author

@kartik-v Sorry about my comming again. I was too early with positiv feedback. Container setting works if nothing overrides later. (eg. ActiveForm config).

But pluginOptionswill be overriden by DateControl completely.

  • In config file I set the container like:
\Yii::$container->set('kartik\widgets\DatePicker', [
    'type' => 2,
    'pluginOptions' => [
        'autoclose' => true,
        'keyboardNavigation' => false,
    ],
]);
  • In DateControl module settings I config autoWidget as you wrote to true

DateControl overrides pluginOptions with 'format' => 'd.m.Y'. The previous entries from container are overridden. It doesn't merge the array.

I tried to set format in config with Yii::$app->formatter->dateFormat;. This isn't working because object isn't loaded application->run hasn't been called yet.

So I tried setting default values with params und autoWidget = true. I call DateControl as follows:

                        <?= $form->field($model, 'rcv_out_date')->widget(DateControl::className(), [
                            'type'=>DateControl::FORMAT_DATE , 'options' => Yii::$app->params['kv-DatePicker']

Now, it works. pluginOptions has a merge now with format from DateControl and others from params. It's a little bit complicate for developers without a wiki.

@kartik-v
Copy link
Owner

@erik-r I mentioned Yii::$container as an example for setting up global defaults for any class extending from Yii object.

But for the datecontrol module - its simpler, because you can override and setup your settings at the module level as I had mentioned (if you are setting this module, you do not need Yii::$container, as that will be overridden anyway).

Again its relatively simple -- here are the steps to follow for your global defaulting sequence:

  • just configure the DateControl module settings to setup your global settings for display and save formats
  • configure the widgetSettings in the module if you have autoWidget to false
  • you can override all of these global options if needed at the individual DateControl widget level.

You just need to set this up in your module for DatePicker to work - not sure why its complicated (check and let know if the following sample works for you - or what's the error you are facing):

use kartik\datecontrol\Module;
...
'modules' => [
    'datecontrol' => [
        'class' => 'kartik\datecontrol\Module',

        // disable automatic use of kartik\widgets for each of the above formats
        'autoWidget' => false,

        // setup your date picker widget default options
        'widgetSettings' => [
            Module::FORMAT_DATE => [
                'class' => 'kartik\widgets\DatePicker',
                'options' => [
                    'type'=>2,
                    'convertFormat'=>true,
                    'pluginOptions'=>[
                        'format'=>'dd.mm.yyyy',
                        'autoclose' => true,
                        'keyboardNavigation' => false,
                    ],
                ]
            ],
        ],

    // other settings
],

@Enrica-r
Copy link
Author

@kartik-v This I know, but now I see what you don't see. I DON'T want to hard code 'format' => 'dd.mm.yyyy'! If application is used in USA or if user is American the date format will be 'mm/dd/yyyy' dynamicly given by formatter. (Yii::$app->locale). It's not possible to use Yii::formatter->dateFormat as this level.

@kartik-v
Copy link
Owner

@erik-r - I will enhance the autoWidget options with an additional autoWidgetSettings... to configure the kartik\widgets\DatePicker and DateTimePicker at module level (when autoWidget is true). Will update.

@kartik-v
Copy link
Owner

The module has been upgraded to version 1.1.0. Refer change log and documentation - especially the defaulting rules section.

  • You have a new property autoWidgetSettings at module level now which will allow you to set global settings for kartik\widgets when autoWidget is true.
  • You can also now newly setup Yii::$app->params['dateControlDisplay'] and Yii::$app->params['dateControlSave'], which will override the displaySettings and saveSettings at Module level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants