In my database, I have a date of birth (dob) field that is of type date.
When I create model from my table, it creates one attribute as dob for
me.
So when I generate my form from my model, it has one field that can collect
the user’s date of birth. What I want to have is to have 3 dropdown elements
each for day month and year. then when I submit my form, they all get bundle
up together as dd-mm-yyyy and get populated to my model attribute “dob“
At this point I can’t figure out how to do it using Yii form since the model
gets passed to the view, how can I find a way to accomplish this. So to recap,
-I want to have only one field in my User table for dob as date type
-I want to have 3 dropdowns in my Yii generated form that at submit time they
form my dob and populate it in the $model->user_dob
That is really strange… I did a similar thing earlier today. I find that people don’t use the date picker correctly so I prefer to use drop downs.
Declare virtual fields in your model for month, day, and year, add validation rules, then create a drop down for each.
Example of creating the drop down lists:
You need to fill the arrays with the months, days, and years you want to give as options. This seems to be outside the scope of your question but I’ll include example functions for doing that at the end, just in case.
Then in your form controller, when you do:
those values should be added to $model->month, $model->day, and $model->year.
Then all you have to do is format it in the Date format like this:
and save the model.
— extra: example of month, day, year arrays —
These aren’t really the best way of doing it, but for my purposes they have worked fine.