yii\jui\DatePicker buggy

Hello,

It is mine impression or yii\jui\DatePicker class is somewhat buggy?

Some difficulties or bugs I had found:

Using the following syntax (to be compatible with ActiveForm:


    <?= $form->field($model, 'birth_date')->widget(

                                                DatePicker::className(),

                                                [//'language' => 'pt',

                                                 'clientOptions' => [

                                                   // 'dateFormat' => 'yy-dd-mm',

                                                    'changeMonth' => true,

                                                    'changeYear' => true,

                                                    'yearRange' => '-90:+0'

                                                ]

                                                ]) ?>

Language changes me the datepicker locale, however it does change the dateFormat by itself too. However dateFormat in clientOptions do not seem to be working at all, as I can just type strange stuff into the field, and it really doesn’t change anything.

I can’t find neither any way to add additional html options to datepicker, like a class name for example.

Any suggestions?

I had problems with it too. It was ignoring the dateFormat, which meant I couldn’t get it to pass validation. So I wrote a beforeValidate method for the model:







 public function beforeValidate()

    {

        if ($this->birthdate != null) {

                        

            $new_date_format = date('Y-m-d', strtotime($this->birthdate));

            $this->birthdate = $new_date_format;

        }


            return parent::beforeValidate();

     }



I need to save as Y-m-d, and it was only returning MMM d, Y. The only problem with this solution is you can enter gibberish and end up with 1970-01-01 as the date. I didn’t have time to work on it further to account for that, but I will eventually come back to it, or hopefully by then, they will fix the widget.

Hey evercode,

Thanks for answering.

Yeah, currently I left my code without language attribute set (of course the datepicker is not in english and dates are not shown in dd/mm/yyyy as I did want to).

Changing the language to ‘pt’ it would return me the date as I want to - ‘dd/mm/yyyy’ -, however when trying to convert it to MySQL datetime field, using:


date('Y-m-d', strtotime($this->birthdate));

Providing this for example:

04-02-1992 (04 November 1992)

When converting the time, strtotime thinks that my 04 is the month, and 02 is the day. However, this is somewhat easy to solve, by exploding the array, and swapping both. I just didn’t had time, and I was just wanting to see if someone had the problems I had with datepicker :)

Do not set dateFormat under clientOptions. We have DatePicker::dateFormat.

Hey, thanks for your response.

Where should I set that? As a third argument? I was not able to find a list of options I should pass to this format of widget (that implements ActiveForm).

[color=#660066]->widget(DatePicker[/color][color=#666600]::[/color][color=#000000]className[/color]color=#666600,[/color][color=#000000] [/color][color=#666600][[/color][color=#880000]‘language’ => ‘pt’, ‘dateFormat’ => ‘xxx’])[/color]

Thanks, qiang, but I tried it and couldn’t get it to work with:





<?php echo $form->field($model,'birthdate')->widget(DatePicker::className(),['dateFormat' => 'Y-m-d']); ?>




And in the model rules:







[['birthdate'], 'date', 'format'=>'Y-m-d'],

[['birthdate'], 'default', 'value' => null],




I tried commenting out the null rule, doesn’t make a difference. When I test it, I’ve taken out my beforeValidate method, so that is not a factor.

The only response I get from it is when I use:




<?php echo $form->field($model,'birthdate')->widget(DatePicker::className(),['clientOptions' => ['dateFormat' => 'yy-mm-dd']]); ?>



This however, returns a different format for the date, MMM d, Y, which I believe is the default of the widget.

I’m afraid I’m getting the same output as @evernote with your snippet @qiang.

Github: https://github.com/yiisoft/yii2/issues/5857

I have posted an answer on github: https://github.com/yiisoft/yii2/issues/5857#issuecomment-65162906