Hi I need to validate 2 attribute based on 1 special rule. Basically new rule looks like:
public function myRule($attribute)
{
if(//work here)
$this->addError($attribute, 'Wrong');
}
I would like to do something more complex:
public function myRule($attribute1,$attribute2)
{
if(//work here with both 2 attributes)
$this->addError($attribute1, 'Wrong1');
$this->addError($attribute2, 'Wrong2');
}
But it doesnt work. Any better way to mix 2 attributes for one validation?
public function rules()
{
return array(
array('attribute1, attribute2', 'myRule'),
);
}
public function myRule($attribute, $params)
{
if ($someCondition) {
$this->addError($attribute, 'Error');
}
}
public function rules()
{
return array(
array('attribute1, attribute2', 'myRule'),
);
}
public function myRule($attribute, $params)
{
// $attribute1 do stuff with $attribute2 -> $newStuff
if (//$newStuff blah blah here) {
$this->addError($attribute, 'Error');
}
}
The question is how do I know which ones is attribute1 or atrribute2 in these code in order to do stuff.
If I understand, you want to access the values of your current model instance attributes in your rule in order to validate these attributes. Right?
So you could use [font="Courier New"]$this[/font] that represents the model instance. And [font="Courier New"]$this->attribute1[/font] and [font="Courier New"]$this->attribute2[/font] give you those values.