dr24's avatar
Level 2

How to save requests from one form and then pass those request to another form in Laravel?

I have one form where I add some information about company, and when submitted it leads to another form where I want to add some additional information about company and when I submit that other form I go to the third page where when I @dd($request->all()) I want to have requests from both forms. Currently when I dump I only have requests from second form. Any help is appreciated. Here is my code.

First form

<form method="get" autocomplete="off" novalidate>
    <div class="host" id="q-business-address">
        <div>Which is your business address?</div>
    </div>
    <div class="guest" id="a-business-address">
        <div>
            <input class="input input-size guest-color" id="business-address" type="text" name="business_address" placeholder="Please enter your business address" required>
        </div>
        <input class="button is-primary" type="button" onclick="emptyValidation('business-address', 'q-company-name', 'a-company-name')" value="GO">
    </div>
    <div class="host" id="q-trade-name">
        <div>What is your trade name?</div>
    </div>
    <div class="guest" id="a-trade-name">
        <div>
            <input class="input input-size guest-color" id="trade-name" type="text" name="trade_name" placeholder="Please enter your trade name" required>
        </div>
        <input class="button is-primary" type="button" onclick="emptyValidation('trade-name', 'q-either-name', 'a-either-name')" value="GO">
    </div>
    <div class="guest" id="a-terms">
        <div class="given-answers">
            <input class="button is-primary" type="submit" name="terms" value="YES, LAUNCH MY BUSINESS"
                formaction="{{ route('second-form') }}">
        </div>
        <div class="given-answers">
            <input class="button is-primary" type="submit" name="terms" value="NO, I NEED FURTHER INFORMATION BEFORE MAKING A DECISION"
                formaction="{{ route('home') }}">
        </div>
    </div>
</form>

second-form

<form id="additional-form" novalidate onchange="validateAdditionalForm()" action="{{ route('payment-page') }}" method="get" autocomplete="off">
    <div>
        <h1 class="title is-4 text-color" >Your Personal Details</h1>
        <div class="field mt-4 form-group">
            <label class="title is-5 text-color">First Name <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="first_name" placeholder="First Name..." required
                       pattern="/^[A-Za-z ]+$/"  data-pristine-pattern-message="This field can contains only letters">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Last Name <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="last_name" placeholder="Last Name..." required
                       pattern="/^[A-Za-z ]+$/"  data-pristine-pattern-message="This field can contains only letters">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Date Of Birth <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="date" name="date_of_birth" max="{{date("Y-n-j")}}" required>
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Nationality <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="nationality" placeholder="Nationality..." required
                       pattern="/^[A-Za-z ]+$/"  data-pristine-pattern-message="This field can contains only letters">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Street Address <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="street_address" placeholder="Street Address..." required>
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Address Line 2</label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="address_line" placeholder="Address Line 2...">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">City</label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="city" placeholder="City..."
                       pattern="/^[A-Za-z ]+$/"  data-pristine-pattern-message="This field can contains only letters">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Country</label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="country" placeholder="Country..."
                       pattern="/^[A-Za-z ]+$/"  data-pristine-pattern-message="This field can contains only letters">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Postal Code/Zip</label>
            <div class="control">
                <input class="input input-size guest-color" type="text" name="postal_code" placeholder="Postal Code/Zip...">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Email Address <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="email" name="email" placeholder="Email address..." required>
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Mobile Number <span class="required">*</span></label>
            <div class="control">
                <input class="input input-size guest-color" type="tel" name="mobile_number" placeholder="Mobile number..." required
                       pattern="/(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\s*[)]?[-\s\.]?[(]?[0-9]{1,3}[)]?([-\s\.]?[0-9]{3})([-\s\.]?[0-9]{3,4})/g">
            </div>
        </div>
        <div class="field form-group">
            <label class="title is-5 text-color">Phone Number</label>
            <div class="control">
                <input class="input input-size guest-color" type="tel" name="phone_number" placeholder="Phone number..."
                       pattern="/(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\s*[)]?[-\s\.]?[(]?[0-9]{1,3}[)]?([-\s\.]?[0-9]{3})([-\s\.]?[0-9]{3,4})/g">
            </div>
        </div>
    </div>
</form>

Controller

public function getQuestionnaireData(BaseRequest $request)
{
	$validatedData = $request->validate([
      'business' => 'required|in:LTD,LLP',
      'residing' => 'required|in:yes,no',
      'different_address' => 'required_if:residing,yes|string|max:255',
      'business_address' => 'required_if:different_address,no|string|max:255',
      'company_name' => 'required|string|max:255',
      'different_name' => 'required|in:yes,no',
      'trade_name' => 'required_if:different_name,yes|string|max:255',
      'either_name' => 'required|in:yes,no',
      'since_when' => 'required_if:either_name,yes',
      'industry' => 'required',
      'products' => 'required|string|max:255',
      'additional_products' => 'required|string|max:255',
      'offer_outside' => 'required|in:yes,no',
      'which_countries' => 'required_if:offer_outside,yes|string|max:255',
      'other_jurisdiction' => 'required|in:yes,no',
      'country' => 'required_if:other_jurisdiction,yes|string|max:255',
      'date' => 'required_if:other_jurisdiction,yes',
      'identity' => 'required_without_all',
      'name' => 'required|string|max:255|regex:/^[A-Za-z ]+$/',
      'address' => 'required|string|max:255',
      'phone' => 'required',
      'email' => 'required|string|email',
      'consent' => 'required|in:yes,no',
      //'terms' => 'required',
    ]);

    //dd($validatedData);

    return redirect()->route('additional-form');
}
0 likes
19 replies
Shrike's avatar

You need to forward your variables from one form to the other:

// Controller
public function form_one($request)
{
  $formParams = $request->all();
  return view('form_two', compact('formParams'));
}

// View
@foreach ($formParams as $key => $param)
  <input type="hidden" name="{{ $key }}" value="{{ $param }}">
@endforeach
1 like
dr24's avatar
Level 2

@shrike

When I put post, then it redirects me back on form one and it is supposed to return view form-two. Can you help?

Shrike's avatar

Is it an issue with form validation ?

MarianoMoreyra's avatar

@dr24 as @shrike said this behavior seems related to validation.

If you have some rule that is not passing, and you are not rendering errors at the first form view, you might be missing the reason why you are getting return to the first form.

Try temporary disabling validation at the first form controller and see what happens.

shez1983's avatar

depends on your logic in your controllers

dr24's avatar
Level 2

What would be the best way to procede in controller?

dr24's avatar
Level 2

@shez1983

Currently I put post method on first form that leads to controller where I validate data from first form and then return view where is second form with that validated data in compact. Also I added foreach on second form for input hidden.

automica's avatar

@dr24 if you have multiple steps in your forms, the best way is to create two distinct forms.

This will mean you'll need 5 routes

  1. create form for step 1 [GET]
  2. store data for form 1 [POST] - when you hit the end of this method, redirect user to:
  3. create form for step 2 [GET]
  4. store data for step2, carrying id of resource created [PATCH]
  5. success page for completed process [GET]

You should set fields that you want to store in step 2 as nullable in your table.

I also wouldn't recommend passing data from step1 into your step 2 form as hidden fields. There's no need as its already been stored in the table. if you do then its possible that user can change it by editing the HTML DOM.

I would also suggest you have a status field in your table that you set as default draft, and at the end of step 2 you can mark it as completed. This will also allow you the means for users to access the second part of the form again if they bail on that part mid form submission.

dr24's avatar
Level 2

@shez1983 In controller I have only validated data that returns view. I updated code

dr24's avatar
Level 2

Well the problem is, these are completely separated forms, both url and inputs, but I need some values from the first form to be able to do something in second form, so this method I don't think it can work. Correct me if I am wrong :)

automica's avatar

@dr24 if you've already saved company data in your first step, in your second form you'll already have the company id so query that data and pass it back into the blade to use.

eg:

public function step2(Company $company) {
    
    return view('step2',compact('company'));
}
shez1983's avatar

see it all helps.

but WHAT i would do is, in step 1, i would save all the info in the db.. then display form 2 as you technically dont need info from step 1 in step 2 do you? then you save info from step 2...

however if you want to do it the way you said you shouldnt do a redirect.. just do a normal view(step2, ['data' => $request->all()])

dr24's avatar
Level 2

@shez1983 Well that's the point. I need info from form 1 in form 2. That's what I am trying to do. When clicked submit from form 1 to be redirected to form 2 with requests (data) from form 1.

shez1983's avatar

i mentioned what u shud be doing... its not a redirect its a return view(... , with request data). i gave u both options..did u try the 2nd? however even if you need data from form 1 into form 2.. you can still SAVE It in the db, return a model from db and pass that to your 2nd form.. seems like you are a beginner and need to go over some basics

jlrdw's avatar

You can carry some data where needed with session. If that's what you need.

Please or to participate in this conversation.