gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:17:29 24 4
gpt4 key购买 nike

我在两个选项卡上有两个表单,我的问题是当我选择了第二个选项卡并发送我的表单时,如果它有一些错误,它会将我重定向回来,但是在选项卡一个而不是两个,所以我创建了一个变量来控制哪个选项卡应在加载时显示:
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>
当自定义请求中的验证失败时,我正在尝试将参数发送到我的 View :
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']);
}
我怎样才能做到这一点?谢谢

最佳答案

您可以将其闪到 failedValidation 中的 session 中。方法:

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

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

然后在您的 View 中从 session 中检索它:
<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>

关于php - Laravel如何在自动重定向验证后发送参数以查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60339783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com