在laravel中通过验证误导

时间:2016-12-10 18:18:30

标签: php laravel blade laravel-5.3 laravel-blade

我有两条不同的路线:

Route::post('contact', 'PublicController@postContact');
Route::post('inquiry', [ 'as' => 'inquiry', 'uses' => 'PublicController@postInquiry']);

及其方法:

    public function postContact(Request $request){ 

    $token = $request->input('g-recaptcha-response');
    if (strlen($token) >0 ) {
        $this->validate($request, [
        'fname' => 'required',
        'lname' => 'required',
        'phone' => 'required',          
        'email' => 'required|email',
        'subject' => 'min:3',
        'message' => 'min:10']);

    $data = array(
        'fname' => $request->fname,
        'lname' => $request->lname,
        'phone' => $request->phone,
        'email' => $request->email,
        'subject' => $request->subject,
        'bodyMessage' => $request->message
        );

    Mail::send('emails.contact', $data, function($message) use ($data){
        $message->from($data['email']);
        $message->to('rahunn3@gmail.com');
        $message->subject($data['subject']);
    });

    Session::flash('success', 'Your Email was sent sucessfully!');

    return redirect('/contact');
    }
}

    public function postInquiry(Request $request){
    $token = $request->input('g-recaptcha-response');
    if (strlen($token) >0 ){
        $this->validate($request, [
        'fname' => 'required',
        'lname' => 'required',
        'phone' => 'required',
        'country' => 'required',            
        'email' => 'required|email',
        'subject' => 'min:3',
        'message' => 'min:10']);
    $data = array(
        'fname' => $request->fname,
        'lname' => $request->lname,
        'phone' => $request->phone,
        'country' => $request->country,
        'email' => $request->email,
        'subject' => $request->subject,
        'bodyMessage' => $request->message
        );

    Mail::send('emails.inquiry', $data, function($message) use ($data){
        $message->from($data['email']);
        $message->to('rahunn3@gmail.com');
        $message->subject($data['subject']);
    });

    Session::flash('success', 'Your Email was sent sucessfully!');          

        return view('public.pages.thankyou');   
    }           
}

我遇到的麻烦是第二种方法不能正常工作。查询表单在视图中(single.blade.php)。在对表单进行总结之后,应该将用户重定向到view(thankyou.blade.php)视图,但是它会重定向到view(single.blade.php)而不发送电子邮件,并且不会显示任何错误。

如果我删除验证部分:

$this->validate($request, [
    'fname' => 'required',
    'lname' => 'required',
    'phone' => 'required',
    'country' => 'required',            
    'email' => 'required|email',
    'subject' => 'min:3',
    'message' => 'min:10'
]);

从第二种方法postInquiry它完美运行(发送电子邮件和重定向做thankyou.blade.php)

谁能告诉我我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

它正在重定向,因为您的验证失败了。

将以下代码添加到single.blade.php文件的顶部,它将告诉您验证失败的原因

@if($errors->has())
 @foreach ($errors->all() as $error)
  <div>{{ $error }}</div>
 @endforeach
@endif

有关详细信息,请参阅此处:https://laravel.com/docs/5.3/validation#quick-displaying-the-validation-errors