Case sensitive rule?

In the rules definition, there seems to be no option for enforcing case sensitivity. I have an email field in my table and in my rules, I am using the ‘exist’ validator to make sure the email the user provides is valid. However, to be extra safe, I want to make sure that the email the user submits matches exactly with what is stored in the database.

So for example, given the email, then should throw an error by the validator.

I guess you are using mysql… you need to set a case sensitive collation for the email field -

Yes I’m aware of using mysql’s collation, but I was hoping for a solution at the application layer.

You cannot "fix" this in the application layer as the result depends on the mysql collation…

If you would have a reverted problem… how to check all lowercase emails… then you could use a mysql and php functions to convert the field to lowercase and make the check… but there is no function for preserving case.

Then you’ll need a custom rule where you’ll check the exact match. No big deal imho.

Example? :)

I was thinking of something like that in your model:


    public function rules()


        return array(


            array('email', 'checkEmail', 'on'=>'login'),





    public function checkEmail($attribute, $params) {

        if($this->email != '') {

            $dbEmail = User::model()->find('email=:email', array(':email'=>$this->email));

            if($dbEmail == null) {

                $this->addError($attribute, 'Email does not exist in the database.');

            } elseif($dbEmail->email != $this->email) {

                $this->addError($attribute, 'Email case is not conform to the one in the database.');


        } else {

            $this->addError($attribute, 'Email is mandatory.');


        return false;



PS I haven’t tested it. I hope you get the idea. Of course it’s up to you to see how you want to handle the different comparison cases and the error messages you output. For instance, after your tests, you’ll probably just mix the two first comparisons into one, and not telling the user the exact error (wrong email or wrong case).

Yup I get the idea. Thanks!