jui DatePicker

Hi Guys. I see the jui DatePicker widget was updated recently:

[color="#213F4D"][font="Helvetica, arial, freesans, clean, sans-serif,"][size="5"]chanded jui DatePicker widget to use ICU date format[/size][/font][/color]

[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif,"]- use default application formatting for the date value[/font][/color]

[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif,"]- auto adjust to application language and format setting changes[/font][/color]

[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif,"]- be consistent with formatter and date validator

[/font][/color]

[size="2"]So the problem is the widget seems to be reverting to defalut behavior, where the dateformat is set to:[/size]

[size="2"]MMM-d-y[/size]

[size="2"]which formats like this: Sep 20, 2012[/size]

[size="2"]I think it is MMM-d-y because I looked it up on[/size]

icu-project.org

What I had before in my form:





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



[size="2"]And this is what I had in my model in the rules method:[/size]

[size="2"]




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



[/size]

[size=“2”]That no longer works. Instead I get the MMM-d-y format in the form, no matter what I put in the widget. I tried adjusting my model rules to MMM-d-y, but that didn’t work either. I actually like that [/size][size=“2”]MMM-d-y [/size][size=“2”]format better. [/size]

[size=“2”]So I think I have 2 problems. I don’t know the correct syntax for formatting the date in the DatePicker widget and I don’t know the matching rule validation syntax. The DatePicker is really cool and was working great and I would love to continue to use it. Any help here would be appreciated, thanks.[/size]

[color="#213F4D"][font="Helvetica, arial, freesans, clean, sans-serif,"][size="5"][b]

[/b][/size][/font][/color]

So I figured out that the DatePicker widget is not respecting the dateFormat setting, reverting to the default

Yii::$app->formatter->dateFormat

[size="2"]Which is MMM d, Y. I checked the Jquery from the console, and it was something completely different there, so obviously there is a disconnect somewhere.[/size]

The other problem that this created, when I tried validating Yii::$app->formatter->dateFormat in my rules, it worked, but then the date was not being saved in Mysql correctly.

I checked all available docs and gave up on changing the widget. But I still wanted to use it, so I thought I would implement a behavior, but my colleauge thought that was overkill and suggested a before validate method. That worked out really well. Now the user can enter a number of different date formats and it will convert it for them automatically. The one tradeoff is they can enter a non-date and it will set it to a date in 1970. The attribute is birthdate, here is the code:





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 was going to use a DateTime object from PHP for this method, but Yii would not instantiate it, so I relied on strtotime. I’m new at programming, so this might not be pretty code, but I was happy with the result. And I learned something about beforevalidate, it’s a really cool feature. I hope this helps anyone trying to implement the DatePicker widget.

Use this:


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

Did you actually try the code you were suggesting? Just to be sure I didn’t miss anything, and out of curiousity, I tried that combination again. It does not work. Anyway, as I stated in my post, I’ve found another solution that I’m happier with. We should probably consider this thread closed.