I want that when the validator fails go to same part of the register form in the home.blade.php and not to the begining of the page.
Can someone help me to do that? 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' => ".winhelponline/blog/wp-content/uploads/2017/12/user.png?fit=256%2C256&quality=100&ssl=1",
]);
$customization->user_id = $user->id;
$customization->save();
return $user;
}
This is the error on laravel:
Pls i need some help
Error Laravel
I want that when the validator fails go to same part of the register form in the home.blade.php and not to the begining of the page.
Can someone help me to do that? 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./www.winhelponline./blog/wp-content/uploads/2017/12/user.png?fit=256%2C256&quality=100&ssl=1",
]);
$customization->user_id = $user->id;
$customization->save();
return $user;
}
This is the error on laravel:
Pls i need some help
Error Laravel
Share Improve this question edited Jun 28, 2019 at 19:03 Juan Spada asked Jun 28, 2019 at 18:34 Juan SpadaJuan Spada 231 gold badge1 silver badge4 bronze badges 4- Have you made some changes to the default files shipped with Laravel? Which version are you using? I can't seem to reproduce the error with what you've provided. – Chin Leung Commented Jun 28, 2019 at 18:40
- Laravel Framework 5.8.23 – Juan Spada Commented Jun 28, 2019 at 18:58
- @JuanSpada can you share your function where you are redirecting ? – Tithira Commented Jun 28, 2019 at 19:00
- sorry i cudnt post all the controller, i just edit the original post with the funciton – Juan Spada Commented Jun 28, 2019 at 19:01
1 Answer
Reset to default 3Simply remove the following from your validator
function:
if ($validator->fails()) {
return redirect('/#register')
->withErrors($validator)
->withInput();
}
The validator function is expecting an instance of \Illuminate\Contracts\Validation\Validator
to be returned.
So change to:
return Validator::make([
// Your rules here
]);
The controller will automatically redirect back with the errors on failure.
If you want to redirect somewhere else on failure, what you could do is overwrite the register
method by adding the following in your controller:
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());
}