imehesz
(Imehesz)
1
hello,
I have 2 date fields on my form and my goal is to make sure that date2 is larger than date1 …
so far, I found out that I have to extend the CValidator class and create my own, with a function called validateAttribute().
however, obviously, it only validates one attribute at a time. so now, I’m just not sure if this is the right way to go 
is there a way to validate a group of fields together?
thanks,
iM
qwerty
(qwerty)
2
Hi,
look at CCompareValidator.php file how it is implemented.
imehesz
(Imehesz)
3
thanks, qwerty
the solution:
my rules in the model looks like this:
public function rules()
{
return array(
array(
'date1,date2', 'application.extensions.mydatefolder.Mydatevalidator',
),
);
}
and here is Mydatevalidator class:
class Mydatevalidator extends CValidator
{
public $date_first;
public $date_second;
public function validateAttribute( $model, $attribute )
{
$value = $model -> $attribute;
if( $attribute == 'date1' )
{
$this -> date_first = $value;
}
if( $attribute == 'date2' )
{
$this -> date_second = $value;
}
if( $this -> date_first !== NULL && $this -> date_second !== NULL )
{
if( $this -> date_first > $this -> date_second )
{
$model -> addError( $attribute, 'date2 needs to be after date1' );
}
}
}
}
it works 
–iM