简体   繁体   中英

How to show error messages for a user's email on a form if not saving record

Is there a way to add error messages to an attribute and show them on the form without trying to save the object.

A manager may wish to change the email of a user assigned to a project.

If they change the email to that of another user who is already assigned to a project I want to add an error to the user email attribute and display it.

In this case I would not save the record or update it I would just go back to the form and show the error.

Do I just do

@user.add(:email, "This user is already assigned to this project")

I don't run @user.save or @user.update_attribute, this user is not being updated but being assigned to the project in this case.

I don't just want to show a flash message as I know I could do this. I want the field to show the error.

Is this possible?

Sure, call valid? on the model.

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