我正在使用L5存储库并且它有验证程序包(https://github.com/andersao/laravel-validator)但是我有问题要更新它的唯一字段的验证,有人帮我吗?
******我在RULE_UPDATE以这种方式尝试过但是没有成功的例子......
(((((FIELDS我想验证:people_id,电子邮件,因为这些仅仅是对于用户的休息,而不是当前用户的更新)))))))
在UserService.php中调用验证:
使用Prettus \ Validator \ Contracts \ ValidatorInterface;
public __construct(){
$this->validator = \App::make('App\Validators\UserValidator');
}
save($data){
if(!empty($data['id'])){
$this->validator->with($data)->passesOrFail(ValidatorInterface::RULE_UPDATE);
}else{
$this->validator->with($data)->passesOrFail(ValidatorInterface::RULE_CREATE);
}
}
命名空间App \ Validators;
使用\ Prettus \ Validator \ Contracts \ ValidatorInterface; 使用\ Prettus \ Validator \ LaravelValidator;
类UserValidator扩展了LaravelValidator {
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'people_id' => 'required|unique:users',
'active' => 'required',
'available_chat' => 'required',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'masters_id' => 'required',
],
ValidatorInterface::RULE_UPDATE => [
'id' => 'required',
'people_id' => 'required|unique:users,id,:id',
'active' => 'required',
'available_chat' => 'required',
'email' => 'required|string|email|max:255|unique:users,id,:id',
//'password' => 'required|string|min:6|confirmed',
//'masters_id' => 'required',
],
];
protected $messages = [
'id.required' => 'Usuário não encontrado!',
'people_id.required' => 'Pessoa é obrigatório',
'people_id.unique' => 'Pessoa já cadastrada',
'active.required' => 'Obrigatório',
'available_chat.required' => 'Obrigatório',
'email.required' => 'Digite o e-mail',
'password.required' => 'Digite a senha',
'password.min' => 'Digite uma senha com 6 caracteres',
'password.confirmed' => 'Confirme a senha corretamente',
'email.unique' => 'E-mail já cadastrado',
];
}
答案 0 :(得分:2)
更新时,您需要强制使用唯一的规则来忽略给定ID
'people_id' => 'required|user,people_id,'.$id
//id is the primary id of the field which you want to update
with unique validation which must be ignored
答案 1 :(得分:2)
尝试在方法setId
中输入寄存器的id$this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
答案 2 :(得分:0)
java.awt.EventDispatchThread.run