Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Step Registration in Laravel 5.1

Tags:

I'm trying to build multi-step registration form.

I have a route /register

Step 1 I'm posting this form to step2

route('register', ['step' => 1]) 

enter image description here

Step 2

And i'm getting data of step1 and put inside hidden inputs. And posting to third step for ending registration. If it is successful no problem. But what happened if registration fails ?

route('register', ['step' => 2]) 

Step 3

route('register', ['step' => 3]) 

If Registration fails i'm redirecting user to step2.

Here is my redirect code.

    $new_user = $request->all();     $validator = Validator::make($new_user, $this->rules());      if ($validator->fails())     {         return redirect()->back()             ->withInput($new_user)             ->withErrors($validator->getMessageBag()->toArray());     }     else     {         //     } 

If validation fails i'm redirecting to step2 everything ok. But i'm seeing forms in picture (Step 1) But my uri is /register?step=2

What is the problem. Where am i making mistake ?

UPDATE: (Route Definitions)

Route::get('/register', [     'uses'       => 'Auth\AuthController@getRegister',     'as'         => 'register',     'middleware' => ['guest'], ]);  Route::post('/register', [     'uses'       => 'Auth\AuthController@postRegister',     'middleware' => ['guest'], ]); 

UPDATE 2: (getRegister and postRegister)

Note: I didn't finished coding getRegister and postRegister.

getRegister

public function getRegister(Request $request)     {         if(!$request->has('step'))         {             /**              * Eğer kayıt ekranında ?step=1,2 vs. yoksa direk ?step=1 e yönlendirme yapıyoruz.              */             return redirect()->route('register', ['step' => 1]);         }          $countries = (new LocationCountry)->getAllCountries()->toArray();         foreach($countries as $key => $country)         {             $countryNames[$key] = $countries[$key]['translation'] = trans('country.'.$country['code']);         }          array_multisort($countryNames, SORT_STRING, $countries);          /**          * Ülke ve Zaman Dilimi için Varsayılan Seçimi          */          $default = new \stdClass();          $default->country = (Lang::locale() == 'tr') ? 'TR' : 'US';          $default->timezone = (Lang::locale() == 'tr') ? 'Europe/Istanbul' : 'America/New_York';          $timezones = (new DateController)->getTimeZoneList();          return view('auth.register.index', compact(['timezones', 'countries', 'default']))             ->with('orderProcess', TRUE);     } 

postRegister

public function postRegister(Request $request){         if(!$request->has('step'))         {             /**              * Eğer kayıt ekranında ?step=1,2 vs. yoksa direk ?step=1 e yönlendirme yapıyoruz.              */             return redirect()->route('register', ['step' => 1]);         }          if ($request->get('step') == 2)         {             $new_user = $request->all();              $new_user['tc_citizen'] = (!isset($new_user['tc_citizen'])) ? 0 : 1;             $new_user['area_code']  = (new LocationCountry)->getCountryAreaCodeByCode($new_user['country']);              $cities = (new Location)->getCities();              /**              * Eğer Post Durumunda ise ve town değişkeni varsa...              */             if($request->has('town'))             {                 $towns = (new Location)->getTowns($request->get('city'));                  if(!$towns->isEmpty())                 {                  }             }              return view('auth.register.step2', compact(['new_user', 'cities']))                 ->with('orderProcess', TRUE);         }          if($request->get('step') == 3)         {             /**              * Kayıt Sonuç Sayfası              */             $new_user = $request->all();             $validator = Validator::make($new_user, $this->rules());              if ($validator->fails())             {                 return redirect()->back()                     ->withInput($new_user)                     ->withErrors($validator->getMessageBag()->toArray());             }             else             {              }         }     } 
like image 571
Cihan Küsmez Avatar asked Nov 16 '15 20:11

Cihan Küsmez


1 Answers

It's because your redirection is telling the browser to do a GET request on the register?step=2 URL. And in your getRegister method you don't check for the step value (hence you see the same form as for GET step=1).


I see two possible solutions:

  • either you tweak your redirection so that it does a POST request to step=2 (might be tricky)
  • or you serve a different page for GET request to step=2

I would advise you for the second option:

  • The form in step=1 should do a POST to step=1, which should redirect to a GET step=2 if everything is fine (using flash cookie to pass the variables)
  • The form in step=2 should do a POST to step=2, which should redirect to a GET step=3 if everything is fine (using flash cookie to pass the variables)
like image 197
oliverpool Avatar answered Oct 24 '22 12:10

oliverpool