我在Laravel 5中有一个简单的联系表单。如果我用空字段提交它,页面会重新加载,但它没有显示验证的错误消息。如果我填写表格并提交,我会收到此错误:
FileViewFinder.php第140行中的InvalidArgumentException:查看未找到[emailils.message]。
那是因为我没有这样的观点,我不知道该放什么,而Laravel的文档在这个问题上并不清楚:
传递给send方法的第一个参数是应该用作电子邮件正文的视图的名称。
不确定这意味着什么。
路线
Route::get('contact','HomeController@contactus');
Route::post('contact_request', 'HomeController@contactRequest');
的HomeController
public function contactus()
{
if (Auth::check()) {
$count = Cart::count();
} else {
$count = 0;
}
$contact = Contact::all();
return view('contact')->with('contact', $contact)->with('count', $count);
}
public function contactRequest() {
if (Auth::check()) {
$count = Cart::count();
} else {
$count = 0;
}
$contact = Contact::all();
//Get all the data and store it inside Store Variable
$data = Input::all();
//Validation rules
$rules = array (
'name' => 'required',
'email' => 'required|email',
'message' => 'required|min:5'
);
//Validate data
$validator = Validator::make ($data, $rules);
//If everything is correct than run passes.
if ($validator -> passes()){
Mail::send('emails.message', $data, function($message) use ($data)
{
//$message->from($data['email'] , $data['first_name']); uncomment if using first name and email fields
$message->from('feedback@gmail.com', 'feedback contact form');
//email 'To' field: cahnge this to emails that you want to be notified.
$message->to('feedback@gmail.com', 'John')->cc('feedback@gmail.com')->subject('feedback form submit');
});
// Redirect to page
return view('contact')
->with('message', 'Your message has been sent. Thank You!');
//return View::make('contact');
}else{
//return contact form with errors
return view('contact')
->with('error', 'Feedback must contain more than 5 characters. Try Again.')
->with('contact', $contact)
->with('count', $count);
}
}
查看
<h1 class='siteh1'>Contact Us</h1>
<hr class='sitehr'/>
<div class="col-md-6 siteleft">
<h2 class='siteh2'>Contact Info</h2>
<div class='contaddr'><label>Address:</label> {!!$contact[0]['Address']!!}</div>
<div class='contphone'><label>Phone:</label> {!!$contact[0]['Phone1']!!} | {!!$contact[0]['Phone2']!!}</div>
<div class='contemail'><label>E-mail:</label> {!!$contact[0]['Email']!!}</div>
<div class='contfollow'><label>Follow us:</label><p>
<a href="{!!$contact[0]['Facebook']!!}"><img src="images/fb.png" alt="Facebook"></a>
<a href="{!!$contact[0]['Youtube']!!}"><img src="images/youtube.png" alt="Youtube"></a>
<a href="{!!$contact[0]['Skype']!!}"><img src="images/skype.png" alt="Skype"></a>
<a href="{!!$contact[0]['Google']!!}"><img src="images/gplus.png" alt="Google Plus"></a>
</p></div>
</div>
<div class="col-md-4 col-md-offset-2 col-sm-12 col-xs-12">
<p class='siteright'>
Get in touch with us
</p>
<div class='contform'>
{!! Form::open(['action' => 'HomeController@contactRequest', 'METHOD' => 'PUT']) !!}
<ul class="errors">
@foreach($errors->all('<li>:message</li>') as $message)
@endforeach
</ul>
<div class="form-group">
{!! Form:: text('name', '', array('placeholder' => 'Your Name', 'class' => 'form-control', 'id' => 'contname', 'rows' => '4' )) !!}
</div>
<div class="form-group">
{!! Form:: text('email', '', array('placeholder' => 'Your Email', 'class' => 'form-control', 'id' => 'contemail', 'rows' => '4' )) !!}
</div>
<div class="form-group">
{!! Form:: text('phone', '', array('placeholder' => 'Your Phone', 'class' => 'form-control', 'id' => 'contphone', 'rows' => '4' )) !!}
</div>
<div class="form-group">
{!! Form:: textarea('message', '', array('placeholder' => 'Message', 'class' => 'form-control', 'id' => 'contmessage', 'rows' => '4' )) !!}
</div>
</div>
<div class="modal-footer">
{!! Form::submit('Submit', array('class' => 'btn btn-primary')) !!}
{!! Form:: close() !!}
</div>
</div>
答案 0 :(得分:2)
电子邮件只是刀片模板,点注意目录结构。刀片模板存储在resources/views
中,因此在这种情况下,emails.message
会使Laravel在resources/views/emails
中查找文件message.blade.php
。
使用邮件内容创建文件resources/views/emails/message.blade.php
。
表单中的所有数据都是通过$data
参数传递的,因此您应该能够在消息视图文件中访问它。
Hello {{ $name }}, your phone number is {{ $phone }}
就主视图模板而言,您的错误处理代码应为:
@foreach($errors->all() as $message)
<li>{{ $message }}</li>
@endforeach`