Date validation inside rules() method of Model

Hi,

How can I do a date validation inside rules() method of model class(Check date is in correct format and day, month and year has correct values).

Is it possible to call a PHP method such as checkdate inside rules method.

Thanks,

Chamal.

This is not built in, but it can still be done pretty easily. You could use the ‘match’ validator which checks for a given regular expression, or you could create your own custom validator (just create a function in your model and specify the function name as validator in your rule). You can use checkdate in that custom function if you want.

In fact it is built in, you just have to use CTypeValidator with type "date" and set the dateFormat to the desired format. For example:




public function rules()

{

	return array(

		array('myDate', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),

	);

}



i guessing your mean is follow this:


public function rules()

{

    return array(

        array('dateField', 'myCheckdate',),

    );

}


public function myCheckdate($attribute,$params)

{

    if(!$this->hasErrors())

    {

        if(! strtotime($this->{$attribute}))

        {

            $this->addError($attribute,'The date is incorrect.');

        }

    }

}

Can any one tell me how to add validation to date along with the time

There is some format like "yyyy-MM-dd" for date as in

public function rules()

{

    return array(


            array('myDate', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),


    );

}

But wat format shud we follow to add time in this

here you have to use ‘datetime’ type, instead of ‘date’

Hi all,

I am using CJuiDateTimePicker extension for my date field in form…

How to validate it using function rules()?

Thanks.

Date validation Rules


array('date', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),