2017-09-27 40 views
0

,所以这是我的注册控制器laravel登记:添加错误校验外部生成验证

protected function validator(array $data) 
{ 

    return Validator; 
} 

/** 
* Create a new user instance after a valid registration. 
* 
* @param array $data 
* @return User 
*/ 
protected function create(array $data) 
{ 
    register here 
} 

我想转诊系统添加到这个过程基本上当注册用户可以发送一个refer_id(ID的用户的谁曾提到这个用户的网站),我会检查引用ID,如果它是有效的,我会做一些我的事

我想改变我的验证功能,像

protected function validator(array $data) 
{ 

    $validation = Validator::make($data, [ 
     'email' => ['required' ,'email' , 'max:255', Rule::unique('users')->where('rep_id' , $this->rep->id) ] , 
     'password' => 'required|string|min:6|confirmed', 
     'name' => 'required|max:255', 
     'last_name' => 'required|max:255', 
     'refer_id' => 'present|numeric', 

    ]); 

    if(isset($data['refer_id'])) 
    { 
     $refer = User::find($data['refer_id']) ; 
     if($refer) 
     { 
      // return error : refer id is wrong ! 
     } 
    } 

    return $validation ; 
} 

我的问题是这部分

// return error: refer id is wrong! 

我怎么能返回与此错误返回查看或此错误添加到验证错误注册用户?

回答

2

Laravel有一个干净的方式来做到这一点

试试这个

'refer_id' => 'nullable|exists:users,id' 

或可能

'refer_id' => 'present|numeric|exists:users,id'