gpt4 book ai didi

php - 如何在验证 Controller 失败后启用提交按钮 - Laravel

转载 作者:搜寻专家 更新时间:2023-10-31 20:56:46 24 4
gpt4 key购买 nike

提交表单时,我的数据在 Controller 中得到验证。调整导致验证失败的字段后,将无法再提交(启用提交按钮)。在我的网络监视器中,我得到了代码 302(重定向失败,我无法摆脱它)只有在重新加载页面时才能提交数据,但随后所有数据都会丢失,用户必须重新填写表单。验证失败后如何重新提交数据?

在 ProjectController.php 中,您将找到字段的验证。我想在验证失败后我就退出了表格。我需要再次进入表格才能提交。感谢您的帮助。

项目 Controller .php

  public function store(Request $request, Project $project)
{
// Validate fields
$validator = Validator::make($request->all(), [
'name' => 'required|min:3',
'mail' => 'required|email',
'intern_extern' => 'required',
'institute' => 'min:2',
'project' => 'required|min:3',

]);

if ($validator->fails()) {
return redirect('/projects/create')
->back()
->withErrors($validator)
->withInput();
}

$project = new project();
$project->name = request('name');
$project->mail = request('mail');
$project->intern_extern = request('intern_extern');
$project->institute = request('institute');
$project->project_name = request('project');
$project->status = project::STATUS_NEW;

$project->save();

return redirect()->back()->with('message', 'New project is create');
}

创建.blade.php

<form action="/projects" method="POST" id="create_project" >
@csrf
@include('error')


<!-- Create a New project -->
<div class="card">
<h1 class="card-header text-center">Create New project</h1>

<!-- Start contact info -->
<div class="card-body">
<h3 class="card-title">Contact info</h3>
<div class="card-text">

<!-- Start row -->
<div class="row">
<div class="col-2">
<div class="d-flex">
<div class="p-1">
<label class="p-2" for="name">Name*</label>
</div>
</div>
</div>
<div class="col-4">
<div class="d-flex">
<div class="flex-fill p-2">
<input type="text"
class="form-control input-text"
placeholder="name"
name="name"
value="{{old('name')}}"
required
>
</div>
</div>
</div>
<div class="col-2">
<div class="d-flex">
<div class="p-1">
<label class="p-2" for="mail">Email*</label>
</div>
</div>
</div>
<div class="col-4">
<div class="d-flex">
<div class="flex-fill p-2">
<input type="email"
class="form-control input-text"
placeholder="Email"
name="mail"
value="{{old('mail')}}"
required
>
</div>
</div>
</div>
</div> <!-- End row -->

<!-- Start row -->
<div class="row">
<div class="col-2">
<div class="d-flex">
<div class="p-1">
<label class="p-2" for="intern_extern">Intern*</label>
</div>
</div>
</div>
<div class="col-4">
<div class="d-flex">
<div class="flex-fill p-2">
<select name="intern_extern" class="form-control" id="YesNo" onchange="displayInstitute()" required>
<option>{{old('intern_extern')}}</option>
<option id="isYes">Yes</option>
<option id="isNo">No</option>
</select>
<p><font id="result" color="dodgerBlue"></font></p>
</div>
</div>
</div>
<div class="col-2">
<div class="d-flex">
<div class="p-1" >
<label id="institute_label" class="p-2" for="institute" style="display: none;">Institute*</label>
</div>
</div>
</div>
<div class="col-4">
<div class="d-flex">
<div class="flex-fill p-2">
<select name="institute" class="form-control" id="ifyes" style="display: none;">
<option>{{old('institute')}}</option>
<option>QZA</option>
<option>PB</option>
<option>LET</option>

</select>
<input type="text" id="ifno" style="display: none;"
class="form-control input-text"
name="institute"
>
</div>
</div>
</div>
</div> <!-- End row -->
</div> <!-- End text -->
</div> <!-- End body -->
</div> <!-- End card -->

<!-- project -->
<div class="card">
<div class="card-body">
<h3 class="card-title">project</h3>
<div class="card-text">

<!-- start row -->
<div class="row">
<div class="col-2">
<div class="d-flex">
<div class="p-1">
<label class="p-2" for="project">Name*</label>
</div>
</div>
</div>
<div class="col-4">
<div class="d-flex">
<div class="flex-fill p-2">
<input type="text"
class="form-control input-text"
placeholder="Project Name"
name="project"
value="{{old('project')}}"
required
>
</div>
</div>
</div>
</div> <!-- End row -->

<div class="row">
<div class="col-4 offset-4">
<button type="submit" class="btn btn-primary btn-block" style="margin: 10px;">New project</button>
</div>
</div>
</div> <!-- End body -->
</div> <!-- End card -->
<!-- Submit field -->

</form>

最佳答案

如果主验证器中的任何验证失败,它会将您重定向回上一页。如果您在函数内部有任何验证,您可能需要使用

return redirect()->back()->withErrors($validator)->withInput();

或者简单地使用

return redirect()->back()->withInput();

获取所有输入的值。试试这个,如果有任何错误,请告诉我。

编辑你不必使用

if ($validator->fails()) {
return redirect('/projects/create')
->back()
->withErrors($validator)
->withInput();
}

只需留下这些行的评论并尝试。例如,在我的函数中我只是添加了这个并且我没有使用任何 if 条件进行验证。删除变量 $validator 。

$validator = Validator::make($request->all(), [
'name' => 'required|min:3',
'mail' => 'required|email',
'intern_extern' => 'required',
'institute' => 'min:2',
'project' => 'required|min:3',

]);

而不是使用以下方法。

$this->validate($request, [
'registration' => 'required|string|max:30',
'documentType' => 'required'
]);

关于php - 如何在验证 Controller 失败后启用提交按钮 - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787344/

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