0

i have a form in my checkout page, i need insert a new litle form into my exist form, like this:

<form class="login"  id="paypalForm" method="post" action="{{url('payment')}}">

// my inputs checkout....address shipping, email etc...

// i need insert HERE a new litle form to CHECK if input code COUPON is valid


{!! Form::open(['url' => 'coupon']) !!}
<input type="text" class="form-control dark"  id="coupon" name="coupon">
<button type="submit" name="submit" class="btn" />
{!! Form::close() !!}


</form>

-my controller CouponController work good and return the right value

-I tryed do it with blade but it doesnt work, it is going always to "payment" and NOT to route "coupon"

There is a better way? maybe disable one form if other form is submited?

Thank you for your help!

I can not insert form coupon in another place, because the design and layout of my template doesnt permit.

Diego Cespedes
  • 1,353
  • 4
  • 26
  • 47

3 Answers3

2

A form needs one action, so nesting them isn't possible the way you're doing it.

However you have a few options

1) Have two separate forms and submit the coupon first You'd submit the coupon, process it upon success update cart total and return back to the checkout with a flash message of success or failure. Allows for cleaner more focused controllers.

2) Have two separate forms and submit the coupon via ajax. This is pretty common because you don't necessarily want to re-render the page upon an invalid coupon and it also allows for cleaner more focused controllers.

3) Include the form fields in the checkout form You'd then process it in the checkout controller with the other checkout logic. Which is less ideal because you will have pretty lengthy controllers.

Brian Dillingham
  • 9,118
  • 3
  • 28
  • 47
1

You cannot nest <form> elements. HTML explicitly forbids nested forms.

See: Can you nest html forms?

Community
  • 1
  • 1
scrubmx
  • 2,486
  • 19
  • 24
1

I would recommend using an Ajax call for what you are trying to accomplish. You could then make the call to one of your controllers or API to check whether the coupon is valid. This would be ideal because it can give you an instant response. You should not be nesting forms.

Also, you should use the url() function for the action attribute to specify where you want the form to be submitted. This is why you are having troubles with submitting the form to the right url.

If you are not familiar with Ajax (I recommend using jQuery's ajax() function, btw - http://api.jquery.com/jquery.ajax), then onc solution is to simply submit ONE form, with the coupon code, and then validate all data that has been submitted.

If the coupon code expired, then take the user back to the previous page/form and display the appropriate message. Laravel makes it very simple to accomplish this because it allows you to redirect to the previous page with the submitted information and an error message displayed below the input field.

It's actually pretty easy to use and should be part of your controller's method...

$validator = Validator::make($request->all(), [
    'shipping_address' => 'required|max:255',
      ....
    'coupon' => 'your_custom_validation_here'
    // create a custom validation rule
]);

if ($validator->fails() {
    return redirect('url')
        ->withErrors($validator)
        ->withInput();
}

Take a look here for more information... https://laravel.com/docs/5.2/validation

This should be the best and quickest solution, without using Ajax calls.

Custom Validation - https://laravel.com/docs/5.2/validation#custom-validation-rules

goto
  • 4,336
  • 15
  • 20