我希望验证器失败时转到home.blade.php中注册表的同一部分,而不是页面的开头。
有人可以帮我吗? Registercontroller.php
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'phone' => ['required', 'string'],
]);
if ($validator->fails()) {
return redirect('/#register')
->withErrors($validator)
->withInput();
}
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$customization = New Customization();
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($data['password']),
'cuenta' => $data['cuenta'],
'avatar' => "https://i0.wp.com/www.winhelponline.com/blog/wp-content/uploads/2017/12/user.png?fit=256%2C256&quality=100&ssl=1",
]);
$customization->user_id = $user->id;
$customization->save();
return $user;
}
这是laravel上的错误:
我需要一些帮助
答案 0 :(得分:0)
只需从您的validator
函数中删除以下内容:
if ($validator->fails()) {
return redirect('/#register')
->withErrors($validator)
->withInput();
}
验证器函数期望返回\Illuminate\Contracts\Validation\Validator
的实例。
因此更改为:
return Validator::make([
// Your rules here
]);
控制器将在发生错误时自动重定向回去。
如果您想在发生故障时重定向到其他地方,可以通过在控制器中添加以下内容来覆盖register
方法:
public function register(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
return redirect('/#register')
->withErrors($validator)
->withInput();
}
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}