php - How to have multiple fields (D/M/Y) for single date property in Yii? -
i want take user birth day database , there field in table called dob. when created model , crud generated text field dob always. want create 3 inputs.
- for years
- for months
- and dates
so question how add inputs in model's form. thinking of adding new attributes model class there no such attributes in table.
add fields model:
public $year; public $month; public $date;
add these methods model:
protected function afterfind() { parent::afterfind(); $dob = explode('/', $this->dob); $this->year = $dob[0]; $this->month = $dob[1]; $this->date = $dob[2]; return $this; } protected function beforesave() { parent::beforesave(); $this->dob = $this->year .'/'. $this->month .'/'. $this->date; return $this; }
you can use them in cactiveform form:
<?php echo $form->textfield($model, 'year'); ?> <?php echo $form->textfield($model, 'month'); ?> <?php echo $form->textfield($model, 'date'); ?>
Comments
Post a Comment