Date Validations For Datepicker

HI,

I am using a datepicker widget in my form.

How do I validate the date field such that it wont accept any date earlier than today.

You must add your own date validator, read this.

We can use custom function in model for validation.


public function rules()

      {

           return array(

                  array('start_date', 'checkDate','on'=>'create'),

            );

      }


public function checkDate() 

      {

            $strtime=strtotime($this->start_date);

            $newtime=strtotime(date('Y-m-d H:i',strtotime('-1 minute')));

            if($strtime<$newtime) {

            	$this->addError('start_date','Please select a valid date');

            }

      }



You can use widget options or add custom rule.

Widget


<?php

$this->widget('zii.widgets.jui.CJuiDatePicker',array(

    'name'=>'datepicker-month-year-menu',

    'flat'=>true,//remove to hide the datepicker

    'options'=>array(

        'dateFormat' => 'yy-mm-dd',

        'showAnim'=>'slide',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'

        'changeMonth'=>true,

        'changeYear'=>true,

        'yearRange'=>'2000:2099',

        'minDate' => '2000-01-01',      // Set the current date here.

        'maxDate' => '2099-12-31',      // maximum date

    ),

    'htmlOptions'=>array(

        'style'=>''

    ),

));

?>

I have my datepicker which has Y-m-d format.

I have used beforeSave() method to convert it into d-m-Y format.

How do I validate my date field before save?