Laravel如何在自动重定向验证后发送参数以查看
Posted
技术标签:
【中文标题】Laravel如何在自动重定向验证后发送参数以查看【英文标题】:Laravel How to send parameters to view after validation on automatic redirect 【发布时间】:2020-06-05 22:48:21 【问题描述】:我在两个选项卡上有两个表单,我的问题是当我选择了第二个选项卡并发送我的表单时,如果它有一些错误,它会将我重定向回来,但在选项卡一个而不是两个,所以我创建了一个变量来控制加载时应在哪个选项卡中显示:
刀片文件:
<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']);
我怎样才能做到这一点?谢谢
【问题讨论】:
【参考方案1】:您可以在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>
【讨论】:
完全忘记了所有通过重定向发送的都必须通过会话获取,非常感谢!【参考方案2】:试试这个方法:
return redirect(
route('get.user.index', ['param' => $param])
)->with('success', 'Message if you need');
'get.user.index'
- 是你的路线
['param' => $param]
- 是您要放入路由的数组
【讨论】:
不工作它说 Class App\Http\Requests\ContactWithoutBookingRequest 不存在“如果我添加它以上是关于Laravel如何在自动重定向验证后发送参数以查看的主要内容,如果未能解决你的问题,请参考以下文章