Laravel 4 唯一验证规则不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26200274/
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 4 Unique Validation Rules Not Working
提问by piso
need help updating a unique rule in my validation rules. I have a abstract validator that will validate a rules before storing into my database and in the rules array I set the email to be unique when creating or registering a user but when updating the user the enique email should not validate if the email is owned by the user.
需要帮助更新我的验证规则中的唯一规则。我有一个抽象验证器,它将在存储到我的数据库和规则数组之前验证规则,我在创建或注册用户时将电子邮件设置为唯一,但在更新用户时,如果电子邮件属于用户。
abstract class Validator
抽象类验证器
abstract class Validator {
protected $errors;
protected $attributes;
public function __construct($attributes = null)
{
$this->attributes = $attributes ?: \Input::all();
}
public function passes()
{
$validation = \Validator::make($this->attributes, $this->rules());
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
public function getErrors()
{
return $this->errors;
}
}
Validation Rules(UserRule.php)
验证规则(UserRule.php)
use MyCustomValidatorNamespaceHere....
class UserRules extends Validator
public function rules()
{
return [
'email' => 'required|email|unique:users,email,id',
...
];
}
and in my UserController I injected the UserRule in the constractor. (UserRule $userRule). Here is the code in the update method.
在我的 UserController 中,我在构造函数中注入了 UserRule。(用户规则 $userRule)。这是更新方法中的代码。
public function update($id)
{
$if ($this->userRule->passes())
{
$this->user->find($id)->update(Input::all());
return .........
}
}
But the validation always fail and displaying the error that the email is already taken. Please help guys.
但是验证总是失败并显示电子邮件已经被占用的错误。请帮助伙计们。
回答by Marcin Nabia?ek
The problem is your rule. When you update, you need to use unique
that doesn't check record you update. So you should have:
问题是你的规则。当您更新时,您需要使用unique
不检查您更新的记录。所以你应该有:
unique:users,email,id
but for example:
但例如:
unique:users,email,10
if you edit record with id 10.
如果您编辑 ID 为 10 的记录。
What you could do is to define this rule:
您可以做的是定义此规则:
'email' => 'required|email|unique:users,email,{id}',
and your passes
method:
和你的passes
方法:
public function passes($id = null)
{
$rules = $this->rules();
$rules['email'] = str_replace('{id}', $id, $rules['email']);
$validation = \Validator::make($this->attributes, $rules);
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
and now in update rule use:
现在在更新规则中使用:
if ($this->userRule->passes($id))
By the way you have error in $if ($this->userRule->passes())
- it should be if
and not $if
顺便说一句,你有错误$if ($this->userRule->passes())
- 它应该是if
而不是$if
回答by Abdalla Arbab
You can use the route
method inside your request class to except an id from the validation
您可以使用route
请求类中的方法从验证中排除一个 id
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->route('user'),
...
];
}
回答by Robbie
I had a problem like that before and it was difficult to find an answer. Here is what I did.
我以前也遇到过这样的问题,很难找到答案。这是我所做的。
class UserRules extends Validator {
public function __construct($input = NULL) {
$this->input = $input ?: \Input::all();
if(isset($this->input['id'])):
self::$rules['username'] = 'required|unique:users,username,'.$this->input['id'];
self::$rules['email'] = 'required|email|unique:users,email,'.$this->input['id'];
else:
self::$rules['username'] = 'required|unique:users';
self::$rules['email'] = 'required|email|unique:users';
endif;
}
public static $rules = array(
'company_id' => 'required',
'role' => 'required',
'password' => 'sometimes|required|confirmed'
);
}
You need to use self::
because $rules is static.
您需要使用,self::
因为 $rules 是静态的。
回答by Cristian Iosif
I have moved this function:
我已经移动了这个功能:
public function passes($id)
{
$rules = static::$rules;
$rules['username'] = str_replace('{id}', $id, $rules['username']);
$rules['email'] = str_replace('{id}', $id, $rules['email']);
$validation = \Validator::make($this->attributes, $rules);
if($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
into UserRule.php and commented the same function in abstract class Validator Now updating is working.
进入 UserRule.php 并在抽象类 Validator 中注释相同的函数现在更新正在工作。
回答by cgross
I solved this problem here on stackoverflowin a generic way. It will automatically adapt your rules if you do an update and add exceptions to your :unique
if necessary.
我在 stackoverflow上以通用方式解决了这个问题。如果您进行更新,它会自动调整您的规则,并:unique
在必要时向您添加例外。