首页 文章

在页面重定向的路线中表单提交/验证 - Laravel

提问于
浏览
0

在我的刀片文件中

<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是初学者,但是这可能使用框架吗?

1 回答

  • 0

    您从表单发布到路由

    method="POST"
    

    但是你的路线被定义为GET

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

    将其更改为POST

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

相关问题