最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call to undefined method IlluminateHttpRedirectResponse::validate() - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 3

Simply 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());
}
发布评论

评论列表(0)

  1. 暂无评论