简体   繁体   中英

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

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!

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."),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM