Solution for using DatePickers on forms in Yii2

Sometimes in web software applications we need to collect dates such as a start date or end date using forms. In this article I will be using the Kartik DatePicker widget which can be found here …

http://demos.krajee.com/widget-details/datepicker

For the purposes of this article a master Model will need to be created. I discussed how to create master Models and Controllers in the following article …

Create a master Model and Controller in Yii2

So based on the master Model now being created the article can continue. Add the field to the form using code similar to this. Include the plugin in the form …

Add the field to the form …

In the master Model 2 methods needed to be created …

And …

These methods can be called to save the “date” data to the database from any of the child Models. The best place to call the methods would be in the “beforeSave” and “afterFind” methods as follows …

And …

Depending on how you have setup the validations for the “date” attribute you may need to run the “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods as well as the “beforeSave” and “afterFind”.

This depends on whether your validations are formatting the date as the date would be saved to the database or validating against the format the date appears on the form.

If your validations validate against the format the date saves to the database then calling “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods will not be needed.

I generally setup my validations so I do not need to run the “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods only through the “beforeSave” and “afterFind” methods.

Using the above code you can implement multiple datepickers on the same form or across different forms. Follow the same logic, add the field or fields to the form and call the “saveDateFormat” and “dateFormat” methods from the child Model.

171 Total Views 1 Views Today

2 thoughts on “Solution for using DatePickers on forms in Yii2”

  1. Hello James,
    I am currently updating a website with the Master Model and Master Controller. I like the idea putting the date formatting in master model.

    Could you elaborate a bit more on your statement:
    “I generally setup my validations so I do not need to run the “saveDateFormat” and “dateFormat” methods”.

    Thanks

  2. Hi,

    Yes the “saveDateFormat” and “dateFormat” methods” are the 2 methods we are creating in the master Model of this article.

    The full sentence reads …

    “I generally setup my validations so I do not need to run the “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods only through the “beforeSave” and “afterFind” methods.”

    I am merely saying that I call the “saveDateFormat” and “dateFormat” methods in the “beforeSave” and “afterFind” methods only.

    As I do not need to call the “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods due to the way I have setup my validations.

    Would you like me to elaborate futher?

Leave a Reply

Your email address will not be published. Required fields are marked *