Contradicting Validations

I’ve come across an interesting predicament :)

In my application, I do not want to store the user’s email address in plain text. As a result I end up having to hash it before insertion.

For validation, I am using the following rules:

	array('email', 'email', 'on'=>'register')

	array('email', 'unique', 'on'=>'register')

I can override the beforeValidate() and beforeSave(). So, if on beforeValidate I were to hash the email, then the ‘email’ validator will complain since the hash is technically not a valid email. Okay…so let’s say I were to not hash on beforeValidate() and instead I hash on beforeSave() so that the ‘email’ validator would work on the validate() call. What’s the problem with this approach? We now have the ‘unique’ validator falsely passing because it’s actually comparing the plain text email (since we decided not to hash on beforeValidate) to hashed emails stored in the database, which even though is technically unique, it is not the desired comparison that I want.

It appears that I would have no choice but to manually implement one of the validators, for example: in the afterValidate I hash the email and then manually check in the database if the hash exists yet. But I’m just wondering if there’s any other way to do this? Can anyone think of any clever tricks to get around this? I prefer to utilize Yii’s built-in stuff instead of having to re-invent the wheel.

Create a custom validation rule: a rule that points to a function you are free to write.

Just for curiosity: what are the advantages of saving emails hashed?

No advantage :) I only mentioned this because of its conceptual significance, which is how to handle the situation where you would have conflicting validations. Unfortunately nobody else has given any suggestions so it seems that my theory of manually validating is correct.