For comparisons I use client checking and onBeforeSave to check for the attributes:
public onBeforeSave(){
if (parent::beforeDelete()){
if($model->attribute_id == $model->value)
{
// write error to Model
$model->addError('attribute_id', 'value and attribute_id cannot be the same');
return false;
}
return true;
}
return false;
}
Cheers Antonio, I might have to do it that way but I don’t know why it doesn’t work in the rule - the ‘criteria’ option is provided so that we can define a criteria for the rule to work with. I must be doing something incorrectly somewhere.
I have just done some testing - in my rule that I have posted at the top, if I hardcode in a value, eg:
'params'=>array(':attribute_id'=>1),
Then the rule works. So it does not seem to be understanding/capturing $this->attribute_id
mmm… that is interesting G, that may have something to do with object lifecycle, that is, the attribute is not yet set.
I look around for my side. I would like to do some research before I post, let me know if you find something as I am interested in this solution.
edit:
After doing some research I am thinking that what you don’t want is that attribute_id is not equal a value right? Then, why don’t you use CCompareValidator instead?
Lol this isn’t as complicated as it seems. attribute_id is just a parent of value. We are not doing a comparison between attribute_id and value. This might be more helpful:
This is all valid:
attribute_id value
1 Dog
1 Cat
2 Dog
2 Cat
This is not valid:
attribute_id value
1 Dog
1 Dog
2 Cat
2 Cat
Basically the user cannot enter Dog or Cat twice for the same attribute_id.
But G, can’t you do that on the client? I see this is more like the way you print the form to the user and how it is posted.
Let me explain, I know that I have certain Dogs and Cats (lol) that I can select for a Model right? Then when I display the ‘insert new Model’ I just display those not in relation with Model to be updated. I do not know if I explain my self clearly.
I do this constantly and it works very well, on create I show a list box of the VALUES it can select (checkboxes, listboxes, whatever) on create is easy.
Now on update, I display the VALUES that it has and the possible VALUES he can select. This is now pure javascript, If a user removes an attribute that it has, then list box with possible VALUES to select are updated. On submit, relations are re-created before saving.
If I have Cats, then I display Cat as selected VALUE and Dogs as possible addition to the Model.
Maybe it is too complicated -I come with strong JS background, or my english is very poor, but this is how I do it in these cases.
Antonio - there is no predefined list of values. This functionality is just to ensure that the user does not accidentally enter the same value twice, for the same attribute_id.
P.S. Nothing wrong with your English - it’s very good!
I managed to resolve this by creating my own validation function (not sure why I didn’t think of that before!).
Anyway do you think this is a problem with the CUniqueValidator, or is it never expected to work this way? How I see it is that there is a criteria option provided for this very purpose - all I have done in my custom function is to specify the criteria.