我正在使用Laravel 5.4上的bootstrap3构建创建联系表单。当我单击提交按钮时,我希望将电子邮件发送到我的收件箱,或者如果有错误,则应在后端验证它们,并在表单顶部显示错误。我正在使用Laravel集合来构建表单,当我填写表单并单击提交按钮时,页面仅重新加载并且不进行验证,或者在输入正确的情况下不发送电子邮件。请帮忙吗?
表单部分
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
@if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
@endif
@if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
@foreach($errors->all() as $error)
<li> {{ $error }} </li>
@endforeach
</ul>
</span>
@endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
路线档案
Route::post('/', 'PagesController@postIndex') ->name('index.post');
Route::get('/', 'PagesController@getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info@pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}