skoobi's avatar
Level 13

Undefined variable $validator

Hi. For some reason, the code that I copied and pasted from a working version doesn't work but the exact same code works for another Validation Request...

Heres the Controller ::

use App\Http\Requests\Customer\Dashboard\UpdateForwardingRequest;

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateForwardingRequest $request)
    {
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator, 'forwarding')->withInput();
        }

        $forwarding = CustomerForwarding::where('user_id', Auth::id())->first();
        $forwarding->forwarding_address = $request->get('forwarding_address');
        $forwarding->forward_by_id = $request->get('forward_by_id');
        $forwarding->save();

        $account = CustomerSetting::where('user_id', Auth::id())->first();
        if (!$request->get('poste_restante')) {
            $account->poste_restante = 0;
        } else {
            $account->poste_restante = 1;
        }
        $account->region_id = $request->get('region_id');
        $account->interval_id = $request->get('interval_id');
        $account->forwarding_tel_no = $request->get('forwarding_tel_no');
        $account->save();

        return redirect()->route('customer.dashboard', '#forwarding')->with([
            'status' => 'Your details have been updated',
            'alert' => 'success',
        ]);
    }

The Request::

<?php

namespace App\Http\Requests\Customer\Dashboard;

use Illuminate\Foundation\Http\FormRequest;

class UpdateForwardingRequest extends FormRequest
{
    public $validator = null;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'forwarding_address' => 'required|min:5',
            'region_id' => 'nullable',
            'interval_id' => 'nullable',
            'forwarding_tel_no' => 'nullable|numeric',
        ];
    }


    /**
    * Get the error messages for the defined validation rules.
    *
    * @return array
    */
    public function messages()
    {
        return [
            'forwarding_tel_no.numeric' => 'Please use only numbers.',
        ];
    }


    /**
     * Overrid Handle a failed validation attempt.
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    {
        $this->validator = $validator;
    }
}

On another part, the exact same code works!

Any ideas?

0 likes
1 reply
skoobi's avatar
skoobi
OP
Best Answer
Level 13

Again after posting found the issue. As per usual an User error...

I pasted from the wrong controller.

if (isset($request->validator) && $request->validator->fails()) {
            return redirect()->route('customer.dashboard', '#account')->withErrors($request->validator, 'forwarding')->withInput()->with([
                'status' => 'There was a problem saving. Please try again!',
                'alert' => 'danger',
            ]);
        }

Please or to participate in this conversation.