【问题标题】:Form submission/validation in a route with page redirect - Laravel带有页面重定向的路由中的表单提交/验证 - Laravel
【发布时间】:2014-07-18 21:50:24
【问题描述】:

在我的刀片文件中

<form id="" 
      class="form-horizontal" 
      accept-charset="UTF-8" 
      action="/apply/post/{{$content->vacancy->Id}}" 
      method="POST">

还有我的routes.php

Route::get('/apply/post/{ref}', function($ref) {

// 1. Define some rules and validate...
    $rules =  array('forename' => array('required'), 'email' => array('required'), 'surname' => array('required'), 'address1' => array('required') );

$validator = Validator::make(Input::all(), $rules, array('required' => 'The :attribute is required.') );

if( $validator->fails() ) {
    $messages = $validator->messages();
    return Redirect::to("http://example.co.uk/apply/{$ref}")->withInput()->withErrors($messages);
 } else {
    // do stuff with Input::all()
 }
}

无论我尝试Redirect::to(),即Redirect::back(),在提交表单后,我最终都会被重定向回http://example.co.uk/apply/post/234,这导致我进入未定义的路线和错误页面。

如果用户验证失败,我真的希望他们重定向回http://example.co.uk/apply/234,并显示错误/消息。这样他们就可以重新尝试输入正确的信息,最终将表单提交给http://example.co.uk/apply/post/234 说。

别问,我继承了代码,我的 Laravel 是初学者,但是使用框架可以吗?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您从表单发布到路线

    method="POST"
    

    但是你的路由被定义为 GET

     Route::get('/apply/post/{ref}', function($ref) {
    

    将其更改为 POST

     Route::post('/apply/post/{ref}', function($ref) {
    

    【讨论】:

    • 谢谢。我尝试使用Route::post(),它现在与我对抗:The HTTP status code "0" is not valid.
    • 编辑:现在可以使用Redirect::to("http://example.co.uk/apply/{$ref}")-&gt;withInput()-&gt;withErrors($messages);
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多