RFFRED
4 months ago
227
0
Laravel

Getting form errors in controller

Posted 4 months ago by RFFRED

Hi,

I am building a form with Tabs and if an error is returned, I want to highlight the Tabs that have issues inside them. At the minute, my code does no read the error messsages

    public function edit(Request $request)
    {

        $core_settings = CoreSettings::first();
        
        //checks policy
        $this->authorize('update', $core_settings);

        return view('rf-core-settings::rf-core-settings.core_settings.edit', compact('core_settings'));
  
    }


public function update(CoreSettingsStoreRequest $request)
    {

        //Calls the CoreSettingsStoreRequest where the policy check is done

        $core_settings = CoreSettings::first();  

        // Will return only validated data
        $validatedData = $request->validated();

        //updates the record
        $core_settings->update($validatedData);

        //redirects
        return redirect()->route('admin.dashboard')
                        ->with('success', trans('rf-core-settings::rf-core-settings.core_settings.messages.success_flash'));
    }

When an error occurs, I am redirected to the edit function in the controller and I was wondering if there was a function I could use to fetch my errors based on the code I am using.

I know I can use

$errors = Session::get('errors');

but I was wondering if there was something a bit more refined than going into the session directly.

I have read about bags and I ran little experiment with bags but that did not quite work.

    public function edit(Request $request, Illuminate\Support\ViewErrorBag $view_error_bag)
    {

        $core_settings = CoreSettings::first();
        
        //checks policy
        $this->authorize('update', $core_settings);

        $errors = \Session::get('errors');
        if (!empty($errors)){
              dd($errors);
              dd($view_error_bag);
        }

        return view('rf-core-settings::rf-core-settings.core_settings.edit', compact('core_settings'));
  
    }

The above displayed a bag with errors when using dd($errrors) But no errors in the bag when using dd($view_error_bag);

Any advice?

Please sign in or create an account to participate in this conversation.