在laravel 5.5中提交表单时出错

时间:2018-02-14 20:03:30

标签: laravel routes laravel-5.5

我一个接一个地得到2个错误中的任何一个。

在提交表单时,我收到以下错误

  

由于不活动,该页面已过期。请刷新并重试。

或者此错误

  

InvalidArgumentException

     

路线[注册/用户/图像]未定义。

我已经交叉检查了所有无法找出真正原因的内容。

路线/ web.php

Route::get('/', function () {
   return view('welcome');
});

Auth::routes();

Route::prefix('manage')->middleware('role:superadministrator|administrator|editor|member')->group(function() {
   Route::get('/', 'ManageController@index');
   Route::get('/carezone', 'ManageController@carezone')->middleware('role:superadministrator|administrator')->name('manage.dashboard');
   Route::get('/dashboard', 'ManageController@dashboard')->name('manage.dashboard');
});


Route::middleware('role:superadministrator|administrator|editor|member|subscriber')->group(function() {

   Route::get('register/user/details', 'RegisterUserController@showUserDetailsForm')->name('register.user.details');

   Route::post('register/user/details', 'RegisterUserController@postUserDetailsForm')->name('register.user.details');

   Route::get('/api/username/unique', 'RegisterUserController@apiCheckUniqueUserName')->name('api.username.unique');

   Route::get('register/user/image', 'RegisterUserController@showUserImageForm')->name('register.user.image');

   Route::post('register/user/image', 'RegisterUserController@postUserImageForm')->name('register.user.image');
});


Route::get('/email/unique', 'RegisterUserController@checkUniqueEmail')->name('email.unique');

Route::get('/get/city', 'RegisterUserController@ajaxGetCity')->name('get.city');

Route::get('/registration', 'RegisterUserController@showRegistrationForm')->name('registration');
Route::get('/home', 'HomeController@index')->name('home');

RegisterUserController.php

       /*function to redirect user to user details page after register page*/
   public function showUserDetailsForm() {
      $states = State::all();
      return view('pages.registration.registerUserDetails', ['states'=> $states]);
   }

   /*function to post and save data to the database*/
   public function postUserDetailsForm(Request $request) {
      $validatedData = $request->validate([
         'username' => 'required|alpha_num|min:6|max:20|unique:details,username',
         'dob'      => 'required|date',
         'gender'   => 'required|string',
         'state'    => 'required|numeric',//validation rule for max min values
         'city'     => 'required|numeric',//validation rule for max min values
      ]);

      $post = new Detail;
      $post->user_id = Auth::user()->id;
      $post->username = $request->username;
      $post->dob = $request->dob;
      $post->gender = $request->gender;
      $post->state_id = $request->state;
      $post->city_id = $request->city;
      $post->save();
      return redirect()->route('register/user/image');
   }

我的表格中的小部分

<form id="registerUserDetails" class="form-horizontal clearfix" method="POST" action="{{ route('register.user.details') }}" role="form" novalidate>
{{ csrf_field() }}
.
.
.
.
<button type="submit" class="tabButton">Next</button>
</form>
<script>Javascript Validation here</script>

2 个答案:

答案 0 :(得分:1)

要解决第一个错误,请将csrf字段{{ csrf_field() }}添加到表单

对于第二个,你的路线名称是 register.user.image

所以像这样设置表单的动作

<form action=" {{ route('register.user.image') }} "> 

答案 1 :(得分:1)

要解决第二个错误,您必须在控制器方法(以及任何地方)修复路由名称:

实际值:

return redirect()->route('register/user/image');

正确:

return redirect()->route('register.user.image');