Hi, I am using CActiveForm to create a form that has a dateField. I want to customize the format of the dateField, it is by default: Day/Month/Year. I also want to validate if the date is in the future. Any suggestions???
Thanks!!
Hi, I am using CActiveForm to create a form that has a dateField. I want to customize the format of the dateField, it is by default: Day/Month/Year. I also want to validate if the date is in the future. Any suggestions???
Thanks!!
You can try this:
$pattern = 'dd/MM/yyyy';
$timestamp = strtotime($model->published_datetime);
if ($timestamp !== false)
$model->published_datetime = Yii::app()->getDateFormatter()->format($pattern, $timestamp);
else
$model->published_datetime = '';
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'published_datetime',
'options'=>array(
'dateFormat'=>'dd/mm/yy',
'minDate'=>'+1d',//day can choose >= tomorrow
),
));
in model
protected function beforeValidate()
{
$timestamp = CDateTimeParser::parse($this->published_datetime, 'dd/MM/yyyy');
if ($timestamp === false)
$this->published_datetime = '';
else
$this->published_datetime = date('Y-m-d', $timestamp);
return parent::beforeValidate();
}
if you want to validate in model add rule validate date and use logic
public function rules()
{
return array(
array('published_datetime','validateDate'),
);
}
public function validateDate($attribute,$params){
//allow published_datetime empty
if (!empty($this->$attribute ))
{
if (strtotime($this->$attribute) <= time())
return false;
}
return true;
}