In Yii, how do you validate uniqueness against another column in the table?

148 views Asked by At

I am giving my users the ability to change their account email. To do so, when they submit the new email that they would like to switch to, I am storing that email in my database as a temporary email. Once the user clicks on the confirmation email sent to the new email, their original email will change into their new email. In my "users" table, I have a column, "email," and another one, "temp_email." When a user submits a new email into the "temp_email" column, I would like to validate that it is unique not only within the "temp_email" column, but also within the "email" column.

Currently, I have these two arrays in my rules() function:

array('temp_email', 'email'),
array('temp_email', 'unique', 'message' => UserModule::t("This user's email address already exists."))

which say that the temporary email has to be in email format and that it cannot be the same as any other temporary email. What is the third array I must add saying that the temporary email cannot be the same as any other email in the "email" column? Thank you!

1

There are 1 answers

0
crafter On BEST ANSWER

You can add additional attributes to your rule to specify the exact rules for the unique checking

array('temp_email', 'unique',
      'className' => 'User', 'attributeName' => 'email',
      'message'   => "This user's email address already exists."),