Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

php - Laravel 4 validation email unique constraint

I'm writing a Laravel 4 app that has a users table with the usual contact info. In my users model, my validation for the email specifies 'email'=>'required|email|unique:users', which works fine when registering new users.

My question is how to handle the user edit form-- when the form is submitted, I'd like for the unique email constraint to only be fired if it's not the same as the old email-- otherwise you can't save your profile, since the email (your email) is already inuse.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The third parameter to the unique rule allows you to specify an id of a record to ignore. When you're editing a user, you want your unique validation rule to ignore the value contained by the id of the user you are editing.

'email'=>'required|email|unique:users,email,'.$userId

You can see the docs on the validation rule here.

The trickiest part you'll run into is figuring out how to edit your rule to provide the id of the user you're editing. That all depends on how you have your rules set up and where you're doing your validation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...