Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rogercbe's avatar

Best approach when using Popup Modals

Hello guys, I've been struggling with a good implementation when it comes to use popup modals, specially with forms in them, and laravel. Actually, it's more a general topic rather than just laravel, since it could apply pretty much everywhere.

I've come up with different solutions, but none of them seems to fully convince me, as they both have some drawbacks, maybe I'm missing an obvius one or I'm being too picky!

I've got the modal (bootstrap modal) on a partial view. First thing it came to mind is to include that view on the main page view, then just using js to show it. This would be good if there were only one modal per page, but if I've got for example a table with records and every single one of them has an edit button that triggers the modal so I can edit the item, makes it really inneficcient in my mind, since i would have hundreds of modals loaded in the view, because the include would have to be inside the loop. I've seen this approach very often though.

So the second thought would be using ajax in those cases, a controller method that simply returns the view with the item fetched, and then display it, to show validation errors after submit, a simple count($errors) would call that popup again, and it would display them. This one seems cleaner to me, but looks like a lot of calls and probably a bit slower on execution time.

0 likes
5 replies
bugsysha's avatar

If data is structured same then you can use one modal with ajax calls to populate that form (if you need more data) and edit it.

handy_man's avatar

In my web app I use a large amount of modals to show additional information, or linked information for that matter and have generally seen no slowdowns / massive inefficiency in this approach.

If you are concerned with efficientcy and seeing slowdowns in your application you mention "i would have hundreds of modals" have you considered using Pagination? I paginate my data to make sure it doesn't get out of hand. Have a look at the docs about pagination here: http://laravel.com/docs/5.1/pagination

Rogercbe's avatar

@bugsysha well that is quite interesting. So the form view would be included on the page, then fetching data with ajax and attach it to the form, probably a faster execution time than loading a full view, since it's just returning json data. I'll try that. How would you handle validation in that case?

@handy_man I understand what you mean, yea my table would be somehow paginated, or probably populated with ajax.

bugsysha's avatar

Before submit if no great validation logic is required.

Please or to participate in this conversation.