May 26, 2010, 10:14am
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.
May 26, 2010, 12:01pm
Anyone able to assist? Thanks.
May 26, 2010, 1:20pm
There is not a validator for date.
You can do like that:
* Declares the validation rules.
public function rules()
// name, email, subject and body are required
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.
May 27, 2010, 8:32am
So what’s this then:
May 27, 2010, 9:04am
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?
(Maurizio Domba Cerin)
May 27, 2010, 9:36am