douba9020
2017-07-24 10:52
浏览 52
已采纳

更新期间验证用户电子邮件字段

I am new to laravel.

Currently, I'm working on user table CRUD operations. I've no problems while create, delete and read. I want to update user email column with validation.

When I've made email field as unique, no problem while creating since it will work perfectly with no duplicate email fields.

Validation:

$this->validate($request,[
    'email' => 'required|unique:users'
],
$messages = [
        'required' => 'The :attribute field is required.',
        'unique' => 'The :attribute must be unique',
    ]
);

But, While updating the record, if email field is not changed and other changed, validation fails due to unique violation because of email is checked with record it self.

I want uniqueness in email field even if user email is changed.

Can anyone give me some suggestion to get out of this problem ?

图片转代码服务由CSDN问答提供 功能建议

我是laravel的新手。

目前,我正在处理用户表 CRUD 操作。 我在创建,删除和阅读时没有任何问题。 我想通过验证来更新用户电子邮件列。

当我将电子邮件字段设置为 unique 时,创建时没问题,因为它可以完美地工作,没有重复 电子邮件字段。

验证:

  $ this-> validate($ request,[
'email'=>'  required | unique:users'
 
],
 $ messages = [
'required'=>':属性字段是必需的。',
'unique'=>':属性必须是唯一的'  ,
); 
   
 
 

但是,在更新记录时,如果电子邮件字段未更改且其他更改,则验证因独特违规而失败,因为 电子邮件会自动记录下来。

即使用户电子邮件发生了变化,我也希望电子邮件领域具有唯一性。

任何人都可以给我一些建议 摆脱这个问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyan6958 2017-07-24 10:58
    已采纳

    What you want to do is ignore a specific user ID during the validation process. By passing an ID to ignore to your unique rule, Laravel will exclude that user when checking the uniqueness of the email address.

    https://laravel.com/docs/5.4/validation#rule-unique

    The format of the rule is:

    unique:table,column,except,idColumn
    

    And in your scenario you can use it like this:

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

    Where the $userId in this scenario is the ID of the user you're updating. Usually, this will be present in your request so you should be able to access it from there. If the column you want to ignore is different from id you can add that as the next part of your rule:

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

    An alternative, and more readable syntax for the unique rule in versions 5.3 and above is:

    Rule::unique('users')->ignore($userId),
    
    点赞 打赏 评论
  • douzhanrun0497 2017-07-24 11:02

    Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. You only want to throw a validation error if the user provides an e-mail address that is already used by a different user. To tell the unique rule to ignore the user's ID, you may pass the ID as the third parameter:

       'email' => 'unique:users,email_address,'.$user->id
    
    点赞 打赏 评论
  • doushang1880 2017-07-24 11:04

    I think you can try this :

    'email'     => Auth::check()
                    ? 'required|email|unique:users,email,'.Auth::id()
                    : 'required|email|unique:users,email',
    

    Hope this help for you!!

    点赞 打赏 评论

相关推荐 更多相似问题