Laravel 4验证电子邮件唯一约束 [英] Laravel 4 validation email unique constraint

查看:81
本文介绍了Laravel 4验证电子邮件唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Laravel 4应用程序,该应用程序具有一个users表以及通常的联系信息.在我的users模型中,我对电子邮件的验证指定了'email'=>'required|email|unique:users',,当注册新用户时,该效果很好.

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.

谢谢

推荐答案

唯一规则的第三个参数允许您指定要忽略的记录的ID.编辑用户时,您希望您的唯一验证规则忽略正在编辑的用户ID所包含的值.

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.

您将遇到的最棘手的部分是弄清楚如何编辑规则以提供您正在编辑的用户的ID.这一切都取决于您如何设置规则以及在何处进行验证.

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.

这篇关于Laravel 4验证电子邮件唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆