Laravel 如何在自动重定向验证后发送参数以查看

Laravel How to send parameters to view after validation on automatic redirect

我在两个选项卡上有两个表单,我的问题是当我选择第二个选项卡并发送我的表单时,如果它有一些错误,它会将我重定向回来,但在第一个选项卡而不是第二个选项卡上,所以我创建了一个变量控制加载时应显示哪个选项卡:

blade 文件:

<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>

我正在尝试在自定义请求中验证失败时将参数发送到我的视图:

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'       
        ];
    }
}

我一直在尝试覆盖方法:failedValidation 和 getRedirectUrl,但没有成功...

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']);
}

我怎样才能做到这一点?谢谢

试试看:

return redirect(
    route('get.user.index', ['param' => $param])
)->with('success', 'Message if you need');

'get.user.index' - 是你的路线

['param' => $param] - 是你想放入路由的数组

您可以在 failedValidation 方法中将其闪存到会话中:

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

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

然后在您的视图中从会话中检索它:

<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>