1

I am trying to create a multi-step form for a user to fill after logging in. I created separate pages with forms that will be a part of the common form.

The data will be saved in the "users" table.

I am new to Laravel and I followed this: https://www.5balloons.info/multi-page-step-form-in-laravel-with-validation/

In my FormController I have these methods:

public function index(Request $request)
{
    $request->session()->forget('user');
    $user = User::all();
    return view('form.index',compact('user',$user));
}

public function updateStep1(Request $request)
{
    $user = $request->session()->get('user');
    return view('form.update-step1',compact('user', $user));
}

public function postupdateStep1(Request $request)
{

    $validatedData = $request->validate([
        'first_name' => 'required',
    ]);

    if(empty($request->session()->get('user'))){
        $user = User::where('id',auth()->user()->id)->first();
        $user->fill($validatedData);
        $request->session()->put('user', $user);
    }else{
        $user = $request->session()->get('user');
        $user->fill($validatedData);
        $request->session()->put('user', $user);
    }
    return redirect('/form/update-step2');

}

public function updateStep2(Request $request)
{
    $user = $request->session()->get('user');
    return view('form.update-step2',compact('user', $user));
}

public function postupdateStep2(Request $request)
{
    $validatedData = $request->validate([
        'address' => 'required',
    ]);

    if(empty($request->session()->get('user'))){
        $user = User::where('id',auth()->user()->id)->first();
        $user->fill($validatedData);
        $request->session()->put('user', $user);
    }else{
        $user = $request->session()->get('user');
        $user->fill($validatedData);
        $request->session()->put('user', $user);
    }
    return redirect('/form/store');

}

public function store(Request $request)
{
    $user = User::where('id',auth()->user()->id)->first();
    $user = $request->session()->get('user');
    $user->save();
    return redirect('/form');
}

And these are the Routes:

Route::get('/form', 'FormController@index');

Route::get('/form/update-step1', 'FormController@updateStep1');
Route::post('/form/update-step1', 'FormController@postupdateStep1');

Route::get('/form/update-step2', 'FormController@updateStep2');
Route::post('/form/update-step2', 'FormController@postupdateStep2');

Route::post('/form/store', 'FormController@store');

This is the first part of the form:

@extends('layouts.app')

@section('content')
    <h1>update - Step 1</h1>
    <form action="/form/update-step1" method="post">
        @csrf
        <div class="form-group">
            <label for="name">First Name</label>
            <input type="text" value="{{ old('first_name', $user->first_name ?? null) }}" class="form-control" name="name">
        </div>

        @if ($errors->any())
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        <button type="submit" class="btn btn-primary">Continue</button>
    </form>
@endsection

I get an error when I try to submit, saying that the fields are required. So even if I do enter a name etc., it doesn't work. If I delete the validations, it seems like everything works but no data is added to the database.

Any suggestions?

andrea
  • 39
  • 2
  • 8
  • If the `user` table has `first_name` column then your input name should be `first_name`. AND if you want to update `name` column then change validation name. – Amit Senjaliya Mar 16 '20 at 10:55

1 Answers1

0

You should use the input name is first_name because you have used first_name in the validation.

<input type="text" value="{{ old('first_name', $user->first_name ?? null) }}" class="form-control" name="first_name">

OR

If want to change name value in the user table:

FormController

 $validatedData = $request->validate([
    'name' => 'required',
]);

first part of the form:

<input type="text" value="{{ old('name', $user->name ?? null) }}" class="form-control" name="name">
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24
  • You are absolutely right, I missed that. I changed the input name to 'first_name' and the validation works. However, on the last page, after submission, I get a 404 error... Is something wrong with the store method? – andrea Mar 16 '20 at 11:20
  • Yes: Failed to load resource: the server responded with a status of 404 (Not Found) This is the last form page:
    @csrf
    – andrea Mar 16 '20 at 11:25
  • @andrea please change the route `Route::any('/form/store', 'FormController@store');` Use `any()` method. – Amit Senjaliya Mar 16 '20 at 11:27
  • the 404 was because I made a mistake in the form action. I fix it and it works. Thanks, Amit . The strange thing is that now in the database, only the first_name got saved, and the address didn't... – andrea Mar 16 '20 at 11:46
  • @andrea `address` column exists in the user table? Double check.. – Amit Senjaliya Mar 16 '20 at 12:10
  • @andrea add `dd($request->session()->get('user'));` in the `postupdateStep2` function before return. and check `attributes` array address value changed. – Amit Senjaliya Mar 16 '20 at 12:16
  • Thanks, Amit, I tried but everything seems normal. In fact, even when I switch the order and I add first_name as the second, first_name doesn't get saved... Anyway, maybe you could help me with something else. How would you handle the following: Let’s say I add another part of the form with a question with multiple choices (Yes and No). If the user selects Yes I want to return one page and if it is No I want to return some other page. How can I include that as its own step? – andrea Mar 16 '20 at 17:19