php Laravel:更新时唯一的验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23587833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Validation unique on update
提问by user742736
I know this question has been asked many times before but no one explains how to get the id when you're validating in the model.
我知道这个问题之前已经被问过很多次了,但是没有人解释在模型中验证时如何获取 id。
'email' => 'unique:users,email_address,10'
My validation rule is in the model so how do I pass the ID of the record to the validation rule.
我的验证规则在模型中,所以我如何将记录的 ID 传递给验证规则。
Here is my models/User
这是我的模型/用户
protected $rules_update = [
'email_address' => 'required|email|unique:users,email_address,'.$id,
'first_name' => "required",
'last_name' => "required",
'password' => "required|min:6|same:password_confirm",
'password_confirm' => "required:min:6|same:password",
'password_current' => "required:min:6"
];
models/BaseModel
模型/基础模型
protected $rules = array();
public $errors;
/*
* @data: array, Data to be validated
* @rules: string, rule name in model
*/
public function validate($data, $rules = "rules") {
$validation = Validator::make($data, $this->$rules);
if($validation->passes()) {
return true;
}
$this->errors = $validation->messages();
return false;
}
采纳答案by Ricardo Canelas
One simple solution.
一个简单的解决方案。
In your Model
在你的模型中
protected $rules = [
'email_address' => 'sometimes|required|email|unique:users',
..
];
In your Controller, action:update
在您的控制器中,操作:更新
...
$rules = User::$rules;
$rules['email_address'] = $rules['email_address'] . ',id,' . $id;
$validationCertificate = Validator::make($input, $rules);
回答by Rehmat
Just a side note, most answers to this question talk about email_address
while in Laravel's inbuilt auth system, the email field name is just email
. Here is an example how you can validate a unique field, i.e. an email on the update:
email_address
顺便提一下,这个问题的大多数答案都是在 Laravel 的内置身份验证系统中讨论的,电子邮件字段名称只是email
. 下面是一个如何验证唯一字段的示例,即关于更新的电子邮件:
In a Form Request, you do like this:
在Form Request 中,您可以这样做:
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->user->id,
];
}
Or if you are validating your data in a controller directly:
或者,如果您直接在控制器中验证您的数据:
public function update(Request $request, User $user)
{
$request->validate([
'email' => 'required|email|unique:users,email,'.$user->id,
]);
}
Update:
If you are updating the signed in user and aren't injecting the User
model into your route, you may encounter undefined property when accessing id
on $this->user
. In that case, use:
更新:如果您正在更新已登录的用户并且没有将User
模型注入您的路线,则在访问id
on时可能会遇到未定义的属性$this->user
。在这种情况下,请使用:
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->user()->id,
];
}
A more elegant way since Laravel 5.7 is:
自 Laravel 5.7 以来更优雅的方式是:
public function rules()
{
return [
'email' => ['required', 'email', \Illuminate\Validation\Rule::unique('users')->ignore($this->user()->id)]
];
}
P.S: I have added some other rules, i.e. required and email, in order to make this example clear for newbies.
PS:我添加了一些其他规则,即必需和电子邮件,以便让新手清楚这个例子。
回答by Him Hah
There is an elegant way to do this. If you are using Resource Controllers, your link to edit your record will look like this:
有一种优雅的方法可以做到这一点。如果您使用的是资源控制器,您编辑记录的链接将如下所示:
/users/{user}/edit OR/users/1/edit
/users/{user}/edit或/users/1/edit
And in your UserRequest, the rule should be like this :
在你的 UserRequest 中,规则应该是这样的:
public function rules()
{
return [
'name' => [
'required',
'unique:users,name,' . $this->user
],
];
}
Or if your link to edit your record look like this:
或者,如果您编辑记录的链接如下所示:
/users/edit/1
/用户/编辑/1
You can try this also:
你也可以试试这个:
public function rules()
{
return [
'name' => [
'required',
'unique:users,name,' . $this->id
],
];
}
回答by tewshi
From Laravel 5.7, this works great
从 Laravel 5.7 开始,这很好用
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
回答by Lucas Silva
If i understand what you want:
如果我明白你想要什么:
'email' => 'required|email|unique:users,email_address,'. $id .''
'email' => 'required|email|unique:users,email_address,'。$id.''
In model update method, for exemple, should receive the $id with parameter.
例如,在模型更新方法中,应该接收带有参数的 $id。
Sorry my bad english.
对不起,我的英语不好。
回答by Altravista
an even simpler solution tested with version 5.2
使用 5.2 版测试的更简单的解决方案
in your model
在你的模型中
// validator rules
public static $rules = array(
...
'email_address' => 'email|required|unique:users,id'
);
回答by Janak Bhatta
public function rules()
{
switch($this->method())
{
case 'GET':
case 'DELETE':
{
return [];
}
case 'POST':
{
return [
'name' => 'required|unique:permissions|max:255',
'display_name' => 'required',
];
}
case 'PUT':
case 'PATCH':
{
return [
'name' => 'unique:permissions,name,'.$this->get('id').'|max:255',
'display_name' => 'required',
];
}
default:break;
}
}
回答by Sonam Tomar
Test below code:
测试以下代码:
'email' => 'required|email|unique:users,email_address,'. $id .'ID'
Where ID is the primary id of the table
其中 ID 是表的主 ID
回答by thenguyenit
$rules = [
"email" => "email|unique:users, email, '.$id.', user_id"
];
In Illuminate\Validation\Rules\Unique;
在 Illuminate\Validation\Rules\Unique;
Unique validation will parse string validation to Rule object
唯一验证将字符串验证解析为 Rule 对象
Unique validation has pattern: unique:%s,%s,%s,%s,%s'
唯一验证具有模式:唯一:%s、%s、%s、%s、%s'
Corresponding with: table name, column, ignore, id column, format wheres
对应:表名、列、忽略、id列、格式 wheres
/**
* Convert the rule to a validation string.
*
* @return string
*/
public function __toString()
{
return rtrim(sprintf('unique:%s,%s,%s,%s,%s',
$this->table,
$this->column,
$this->ignore ?: 'NULL',
$this->idColumn,
$this->formatWheres()
), ',');
}
回答by zarpio
Found the easiest way, working fine while I am using Laravel 5.2
找到了最简单的方法,在我使用 Laravel 5.2 时工作正常
public function rules()
{
switch ($this->method()) {
case 'PUT':
$rules = [
'name' => 'required|min:3',
'gender' => 'required',
'email' => 'required|email|unique:users,id,:id',
'password' => 'required|min:5',
'password_confirmation' => 'required|min:5|same:password',
];
break;
default:
$rules = [
'name' => 'required|min:3',
'gender' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:5',
'password_confirmation' => 'required|min:5|same:password',
];
break;
}
return $rules;
}