在更新时验证唯一电子邮件,而忽略自己的电子邮件.在Laravel 5上 [英] Validate unique email on update, ignoring self email. On Laravel 5

查看:58
本文介绍了在更新时验证唯一电子邮件,而忽略自己的电子邮件.在Laravel 5上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel 5.4应用程序,每当我尝试在不更改电子邮件的情况下更新用户时,都会出现错误电子邮件已在使用中",这是有道理的,因为已编辑的用户已经拥有了电子邮件.通常,我只使用类似这样的方法来处理它:

I have a Laravel 5.4 app, whenever I try to update a user with the email unchanged, I get the error 'Email already in use', which makes sense since the edited user already has the email. Usually I deal with it simply by using something like this:

'email' => 'required|max:128|unique:users,email,'.$u->email,

但是,这不适用于该特定应用.我仍然收到相同的已使用中"错误.

However, this is not working on this particular app. I still get the same 'already in use' error.

php 替换字符串后完整的验证规则对象如下所示:

The full validation rules object looks like this after php replaces the string:

$rules = [
  "name" => "required|max:64",
  "email" => "required|max:128|unique:users,email,user@admin.com",
  "role" => "required"
]

我找不到任何问题.是什么原因导致验证不正确?
如果有帮助,我将添加整个更新功能验证过程.

I can't find anything wrong with it. What could be causing the incorrect validation?
I'll add the whole update function validation process, in case it helps.

$u = User::find($id);

$input = $request->all();
$rules = [
  'name' => 'required|max:64',
  'email' => 'required|max:128|unique:users,email,'.$u->email,
  'role' => 'required'
];
$messages = [
  'email.required' => 'El Email es obligatorio',
  'email.max' => 'El Email no debe exceder los 128 caracteres',
  'email.unique' => 'Ya existe un usuario con este Email',
  'name.required' => 'El nombre es obligatorio',
  'name.max' => 'El Email no debe exceder los 64 caracteres',
  'role.required' => 'El Rol es obligatorio',
];
$validator = Validator::make($input, $rules, $messages);
if ( $validator->fails() ) {
  return redirect('users/'.$id.'/edit')->withErrors($validator)->withInput();
} else {
    // update user
}

推荐答案

通过 $ u->电子邮件并不完全适合 unique 规则.签名是

Passing $u->email isn't quite right for the unique rule. The signature is

unique:{table},{column},{value_to_ignore},{column_to_ignore}

因此,将您的规则设置为

So, with your rule set as

unique:users,email,'.$u->email

由于默认 column_to_ignore 设置为 id (或您的 users 表的主键是什么)

It's trying to find a user with an id of $u->email, due to the default column_to_ignore being set to id (or whatever the primary key of your users table is)

要解决此问题,只需更改您的 unique 规则,如下所示:

To fix this, simply change your unique rule as below:

$u = User::find($id);
$rules = [
    "email" => "required|email|max:128|unique:users,email,".$u->id.",id", 
    ...
];

注意:如果愿意,您可以从规则中省略,id" .如前所述,这将默认为您的 users 表的主键.如果您从 id 更改了它,则需要指定它.

Note: You can omit the ",id" from the rule if you like. As said, this will default to the primary key of your users table. If you changed it from id, then you will need to specify it.

使用上述调整的规则,这表示在编辑此User并确定唯一条目时,请忽略 email 列中的值,其中 users.id = $ u->ID .

With the rule adjusted as above, this will signify that when editing this User and determining unique entries, ignore the value in the email column where users.id = $u->id.

这篇关于在更新时验证唯一电子邮件,而忽略自己的电子邮件.在Laravel 5上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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