Ad

Laravel How To Send Parameters To View After Validation On Automatic Redirect

I have two forms on two tabs, my problem is when I have selected the second tab and send my form, if it has some errors it redirects me back but on tab one instead of two, so I created a variable that controls in which tab should be showing on load:

blade file:

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade {{ $tabName == 'yes' ? 'show active' : '' }}" id="pills-yes" role="tabpanel" aria-labelledby="pills-yes-tab">
        @include('secciones.formulario-contacto-reserva')
    </div>
    <div class="tab-pane fade {{ $tabName == 'no' ? 'show active' : '' }}" id="pills-no" role="tabpanel" aria-labelledby="pills-no-tab">
        @include('secciones.formulario-contacto-sin-reserva')
    </div>
</div>

I'm trying to send parameters to my view when validation fails in a custom request:

ContactWithoutBookingRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;

use Illuminate\Foundation\Http\FormRequest;

class ContactWithoutBookingRequest extends FormRequest
{
    /**
     * 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 [
            'inputNameSurname' => 'required|max:255',
            'inputEmail' => 'required|email|max:255',
            'selectMessage' => 'required|numeric|max:2',
            'inputComment' => 'required|max:3000|min:5',
            'privacityCheck' => 'required|boolean',
            'g-recaptcha-response' => 'required|captcha'       
        ];
    }
}

I have been trying to override methods: failedValidation and getRedirectUrl, with no success...

protected function getRedirectUrl()
{
    return redirect()->back()->with(['tabName' => 'no']);
}

protected function failedValidation(Validator $validator)
{
        throw (new ValidationException($validator))
                    ->errorBag($this->errorBag)
                    ->redirectTo(route('contacto'), ['tabName' => 'no']);
}

How can I achieve this? thanks

Ad

Answer

You could flash it to the session in the failedValidation method:

protected function failedValidation(Validator $validator)
{
    $this->session->flash('tabName', 'no');

    throw (new ValidationException($validator))
        ->errorBag($this->errorBag)
        ->redirectTo($this->getRedirectUrl());
}

And then retrieve it from the session in your view:

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade {{ Session::get('tabName') == 'yes' ? 'show active' : '' }}" id="pills-yes" role="tabpanel" aria-labelledby="pills-yes-tab">
        @include('secciones.formulario-contacto-reserva')
    </div>
    <div class="tab-pane fade {{ Session::get('tabName') == 'no' ? 'show active' : '' }}" id="pills-no" role="tabpanel" aria-labelledby="pills-no-tab">
        @include('secciones.formulario-contacto-sin-reserva')
    </div>
</div>
Ad
source: stackoverflow.com
Ad