DateTime Validation

I searched the forum and could not get much help on validating a datetime field. Also it seems that there is no datetime validation built in to Yii…

Basically all I want to do is validate the datetime input on a form. I am using the extension ‘timepicker’ (http://www.yiiframework.com/extension/timepicker/), with this I can pick a date and time and this populates the field.

Ideally I would like the datetime validated in the format: [30-01-2010 14:00] but before being saved it will need to get converted back to MYSQL default datetime format.

Anyone able to assist? Thanks.

There is not a validator for date.

You can do like that:




/**

 * Declares the validation rules.

 */

public function rules()

{

	return array(

		// name, email, subject and body are required

		array('date','dateValidator')

	);

}


public function custom()

{


	if !(checkdate(date('m', strtotime($this->date)), date('d', strtotime($this->date)), date('y', strtotime($this->date)))

		$this->addError('date', 'Date not valid');

}



Or some other way for test the date.

So what’s this then:

http://www.yiiframework.com/doc/api/CTypeValidator

? ;)

Now ideally want the date to be validated in the format: [30-01-2010 14:00]

So basically I did this:


array('event_date', 'type', 'type'=>'datetime', 'datetimeFormat'=>'dd-MM-yyyy hh:mm'),

But upon saving the record I get zero values in my database; this is because the data does not conform to the MYSQL datetime format (YYYY-MM-DD HH:MM:SS).

So how can I make it convert my custom format to the MYSQL format before save?

check this thread http://www.yiiframework.com/forum/index.php?/topic/7371-help-with-date-validation/page__hl__yyyy-mm-dd__fromsearch__1