Ccomparevalidator Isn't Working As Supposed?

Either I don’t understand how CCompareValidator works (sic!) or it doesn’t work for me.

I want to check, if an ID of row / record / user being updated isn’t the same, as an ID of currently logged-in user. And prohibit update, if it is.

I used CCompareValidator at first:

array('id', 'compare', 'compareValue'=>Yii::app()->user->id, 'message'=>'Boom!')

It doesn’t work – it halts editing / update of every row / record / user, no matter, what an ID actually is.

So, I rewrote it to my own, custom validator. In my opinion, the code is the same as in case of built-in one:

array('id', 'compareId', 'compareValue'=>Yii::app()->user->id, 'message'=>'Boom!')

public function compareId($attribute = null, $params = null)


	if($attribute === 'id')


    	if($this->id == $params['compareValue'])






It works like a charm – allows update of any row / record / user, which ID is different than currently logged-in user’s ID. Blocks update, showing defined message, in case compared IDs are equal.

What am I missing? Why original Yii’s built in validator fails on such simple example, while my own works?

I may be misunderstanding, but it seems like the first one would pass when the user ID was the same and fail all other times. The compare validator should be successful when the attribute matches the specified value.

Setting the operator to ‘!=’ might fix it.

Oh, God… you’re right! :expressionless:

I was so sure, that this works exactly opposite way – throws error, if compared values are not equal.

I have never worked with CCompareValidator before and I assumed (from logical point of view only), that it works exactly the same as other validators.

For example. If we use CRequiredValidator, we say (again – logical point of view), that an attribute should be required. And validator throws error, when required attribute is not present (the opposite way). The same with others. We say, that attribute should be numeric or that string should have certain length at most and proper validator throws error message, when attribute isn’t numeric or when string hasn’t got length, that it should have.

And here comes compare validator. We say, that two values should be equal and it throws message, when they are equal (the right way, not the the opposite one). I got confused with that.

EDIT: I’ve asked some of my dev-friends, what is their first feeling and all of them reacted the same way, as I did – compare validator should throw an error, if two compared values are not equal.

I think you’re still misunderstanding it. The compare validator does throw an error if the two values are different (as you expect), but in your specific scenario, you wanted the validator to throw an error when the values were the same, because the user should only be able to edit other user records.

Brain hurts… :lol:

Yes, it seems, that you’re again right.

It should throw an error, when both IDs are the same, while by default, it will only warn, when two ID differs. That’s why I need a negate operator to be set.

Now (I think), I get the entire picture (I hope so). Thanks!