Skip to content
Advertisement

Bootstrap modal validation in Laravel

I have a registrarion modal which opens on click. The inputs are validated on the view using required, etc. Thing is, there are other fields (also in other modals I’m not mentionig) in which I validate again rules in my controller.

I’ve read that a good way of validating modals is using AJAX. I’m having a hard time with that.

This is a simplified verstion of my registration modal which is submitted without javascript or others:

JavaScript

When I submit, the modal closes and nothing happens. If I click on “register” again, the modal shows with the errors showing.

Could someone help me out? Point me in the right direction?

Advertisement

Answer

As far as I understand this, you’re submitting your form normally, which reloads the page. So after the page reloaded, you need to click on “register” again, to open the modal, which correctly shows any errors.

If you want to submit the form normally like this, you have to reopen the modal automatically (using Javascript), if there’s any error. You can open a modal like this:

JavaScript

I hope I understood your problem correctly.

EDIT 1: to open the modal if there is an error, use the following code:

JavaScript

EDIT 2: To submit the form via AJAX and display the errors in the modal directly:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement