O。我需要这个问题的帮助。我在blade.php文件中有一个联系表单,我的web.php文件中有一个路由设置,我有一个控制器设置,该控制器是从web.php文件路由而来,用于在字段上进行验证并显示提交表单后,页面上会显示一条简短的消息。现在,该表单已正确提交到我的数据库,因此可以正常工作,但是如果我使用空白表单提交,则验证无法正常工作(laravel),并且在成功提交表单后也不会显示Flash消息:>
代码:
Web.php
<?php
Route::get('/', 'HomeController@index')->name('home');
Route::post('/contact/submit','MessagesController@submit');
MessagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Message;
class MessagesController extends Controller
{
public function submit(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|min:2',
'email' => 'required|max:255',
'phonenumber' => 'required|min:10|max:10',
'message' => 'required|min:5',
]);
Message::create($validatedData);
return redirect('/')->with('success', 'Message has been sent');
}
}
contact.blade.php
{{--CONTACT FORM--}}
<section id="contact">
<div class="container-fluid padding">
<div class="row text-center padding">
<div class="col-12">
<h2>Contact PDMA</h2>
</div>
<div class="col-12 padding">
{!! Form::open(['url' => 'contact/submit']) !!}
@csrf
<div class="form-group">
{{Form::label("name", 'Name')}}
{{Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Enter name'])}}
</div>
<div class="form-group">
{{Form::label("email", 'E-Mail Address')}}
{{Form::text('email', '', ['class' => 'form-control', 'placeholder' => 'Enter email'])}}
</div>
<div class="form-group">
{{Form::label("phonenumber", 'Phone Number')}}
{{Form::text('phonenumber', '', ['class' => 'form-control', 'placeholder' => 'Enter phone number'])}}
</div>
<div class="form-group">
{{Form::label("message", 'Message')}}
{{Form::textarea('message', '', ['class' => 'form-control', 'placeholder' => 'Enter message'])}}
</div>
<div>
{{Form::submit('Submit Form', ['class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</section>
答案 0 :(得分:1)
只需使用会话: 控制器中的第一个Import Session类
use Session;
Message::create($validatedData);
Session::flash('success', 'Message has been sent');
return redirect('/')
然后在view文件夹中创建一个blade文件,您可以随意调用它,例如:notify.blade.php
@if (Session::has('success'))
<div class="alert alert-success" role="alert" style="bottom:10px; position: fixed; left:2%; z-index:100">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<h4 class="alert-heading">Well done!</h4>
<p>{{ Session::get('success') }}</p>
</div>
@endif
@if (Session::has('danger'))
<div class="alert alert-danger" role="alert" style="bottom:10px; position: fixed; left:2%; z-index:100">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<h4 class="alert-heading">Error!</h4>
<p>{{ Session::get('danger') }}</p>
</div>
@endif
最后,将此文件包含在任何视图中。
include('notify')
答案 1 :(得分:1)
正如利弗森建议为会议创建刀片文件 另外,您还可以捕获错误并使用另一个刀片(例如error.blade.php)更改输入样式,并将其包含在您的表单中
@if($errors->any())
<div class="alert alert-danger" role="alert">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
对于表单,您可以将{{$errors->has('name') ? 'is-danger' : ''}}
添加到div类
例如
<div class="form-row text-left">
<label for="name" class="col-md-3">Name</label>
<div class="col-md-9">
<input type="text" name="name" class="input {{$errors->has('name') ? 'is-danger' : ''}}" required
value= @if(isset($user))"{{$user->name}}"@else "{{old('name')}}"@endif>
</div>
</div>
https://laracasts.com/series/laravel-from-scratch-2018/episodes/15