How to compare two fields in rules function

[font="Times New Roman"][size="4"]i have a model appoint associated with "appint-tbl"[/size][/font]

table has following fields

id(PK), title, client_name, app_date, start_time, end_time, status

[size="4"]now i want to compare [/size][size="4"]start_time with end_time in rules function (in appointment model)[/size]

[size="4"][font="Times New Roman"]conditions are[/font][/size]

[b]start_time < end_time

end_time > start_time

start_time != end_time[/b]

[size="4"]plz tell how to write rules function for these condition in model[/size]

[size="4"]Snapshot: view[/size]




$criteria = new CDbCriteria;

$criteria->select='title, client_name, app_date, status';

$criteria->condition="$start_time<$end_time";

$dataProvider=new CActiveDataProvider('table_name', array(

      'criteria' => $criteria,

      'sort'=>array('defaultOrder'=>'app_date DESC'),

      'pagination'=>array('pageSize'=>20)

));

$this->render('view_name',array(

      'model'=>$model,

      'dataProvider'=>$dataProvider,

));



something like this i think

Hi,

I think you have to add custom validation function. This is how I did it.




// In rules

array('start_time', 'validateTime')


// Add below functions to your model

public function validateTime()

{

	if (!$this->compareTime($this->start_time, $this->end_time))

	{

		$this->addError('start_time', 'Start time should less than End time');

	}

}	




public function compareTime($start, $end)

{

	list($start_hr, $start_min) = str_split(preg_replace('/[:]/', '', $start), 2);

	list($end_hr, $end_min) = str_split(preg_replace('/[:]/', '', $end), 2);

	

	return ($start_hr *60 + $start_min) < ($end_hr * 60 + $end_min);

}



Assume your time input is in hh:mm format.I used CJuiDateTimePicker to select time.

Thanks

Aruna