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.
/**
* 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');
}
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?