Search code examples
phpyii2yii2-basic-appyii2-modelyii2-validation

Yii2. Many inputs for one Model/DB field


How such inputs could be translated into DATE field in DB?

enter image description here

Customer insisted on such way to input user birthdate. First my thought was simply to concat in one field in beforeValidate, but after this inputs won't be able to display date, that was inputed, from the DB. How to avoid such restriction?


Solution

    1. Add these 3 fields as virtual properties in the model class.
    2. In beforeValidate() set the value of birthdate with combination of 3 virtual fields from the form.
    3. Add afterFind() in the model class where value of saved in DB birthdate property is used to set the values of 3 virtual properties.
    4. Now form properly holds 3 fields' values and combined date is saved in DB.