Saturday, March 19, 2022

[FIXED] yii1 date-picker storing values as 000-00-00 in database

Issue

I am tring to use date picker in an form but after submitting, the value is stored as 000-00-00 in database and while Viewing the details after submitting the form the minDate value is displayed .

the same date picker is working fine in other form , I am trying to re-use the date picker

file.js

function userDetailsEventHandlers() {

$('.date-picker').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'dd-MM-yy',
        yearRange: '1900:1998',
        minDate: '01-January-1900',
        maxDate: '31-December-1998',
    });
}

form.php

<div class="form-group">
    <?php echo $form->labelEx($model,'DOB'); ?>
     <?php echo $form->textField($model,'DOB', array('class' => 'form-control date-picker')); ?>
    <?php echo $form->error($model,'DOB'); ?>
</div> 

Model class :

protected function afterFind() {

       if (!empty($this->DOB)) {
            $this->DOB = date('d-F-Y', strtotime($this->DOB));
        }
        return parent::afterFind();
    } 

The datatype in database : datetime (I also tried with date). The datatype in other form where datepicker is used is datetime.


Solution

Finally used CJuiDatePicker, :P

<div class="form-group">
                                 <?php echo $form->labelEx($model,'DOB'); ?>
                                 <?php
                                    $this->widget('zii.widgets.jui.CJuiDatePicker',array(
                                        'model'     => $model,
                                        'attribute' => 'DOB',
                                        'language'=> 'en',


                                        'name'=>'datepicker-month-year-menu',
                                         //'flat'=>true,//remove to hide
                                        'options'=>array(
                                            'dateFormat' => 'yy-mm-dd',
                                            'showAnim'=>'slide',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
                                            'changeMonth'=>true,
                                            'changeYear'=>true,
                                            'yearRange'=>'1900:1998',
                                           // 'minDate' => '2000-01-01',      // minimum date
                                           // 'maxDate' => '2099-12-31',      // maximum date
                                        ),
                                        'htmlOptions'=>array(
                                            'class' => 'form-control',
                                            'style'=>''
                                        ),
                                    ));
                                    ?>

                                     </div>


Answered By - Mukesh

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.