Handle multiple forms at the same timeCreated 4 years ago by cyno
Hi, I have 2 form on a page, and when the user sends one of them I wanna handle both form data (its an ajax form handling). Is it possible? (I've tried addig both form data to the ajax call, but Formbuilder $builder in Handler only gives, the actually sent forms data)
Thank you for your fast and detailed answer! So the thing i'm trying to achieve is ajax filtering on a product page. There are 2 separate forms for it. One of them is a filter with several selects and checkboxes, and the other one is a search bar. Both of them working perfectly separately (I'm using a formbuilder for each of them, and rendering it in the view). The thing is if, you have selected several filters, and type something into the searchbar, it have to search in the already filtered products. Also if u search for something, and than select filters, its gonna filter the results. For this I have to pass the values of both forms to both the handlers of the currently changed form, do the search in the database, and then pass back the view from the call, with the found products. Right now after in the handler i get the values of each builders, inject the entryrepositoryinterface, do the search, and pass back the view as json in $builder->on() function. I haven't tried MultiFormBuilder yet, gonna be my first thing to take a look in the morning. If you have any advices, I would be more than happy to hear them😄
Multiform COULD be your answer. Sounds like you will need to pay attention to the order in which you add for one - first forms are processed first and can pass data through to the other forms from the multiform builder by using the slug of the forms (prefix). So for example on the multiple you could do
onPostingFilters() and use
$this->getChildForm('search') to work with that assuming search was posted first. Might need to dig around in the API a little bit but looks like you can handle yourself with the stuff you're doing 😄
A question I have though is why are you using two forms? Sounds like you could use one. If it's due to view location you could surround more with the single form. You could also use a hidden field for search and use JS to mirror the value and submission action which may be easier to do. Handle it with one form - have the other input wherever mirror content and enter/submission commands to the main filtering one (which now has it's search term mirrored) and handle everything in one. A little work on the front end but it'll probably be a smoother process behind the scenes.
I've personally taken the above approach in a similar situation and it worked really well.
Actually I haven't been thinking about this approach with only one form, and mirroring with JS into a hidden field. To be honest I'm trying to make this application doing everything "Pyroish" to test it, since we are planning to use Pyro for future projects. Also I had to solve a lot of thing for ecommerce, and wanna contribute to your ecommerce package as well, after the product is finished. I'm gonna try the multiform method for sure to see, how it works, and gonna dig a little bit around. But if somethings in the way, will definitely fall back to this one form method. Also thank you for the detailed, and fast answers, always nice to post/read this forum!
Glad you mentioned the ecommerce package! It's actually in a pretty good spot but definitely not ready to use. I am releasing 3.7 within a month or so and will be releasing 4.0 this summer with laravel 5.7 and a new addon marketplace designed to be replaced directly with the ecommerce package later. By the time 4.0 hits I'll have already picked up development on the store again and will be using the ecommerce channel in Slack: https://pyrocms.slack.com/messages/C29MATVHS if you want to join in. Thanks a ton for your interest and let me know if I can help you out any further 😄
Hey there! You sure can do this but just a heads up it's a little advanced. Here's the gist of what you want to do:
Use Multi-Form Builders to Handle Multiple Forms
In your addon - create a MultiformBuilder (or use the base one) and load the other forms into it like this:
$multi->render()or otherwise display the form like you normally would.
This will require BOTH forms to validate - you will have access to manipulate both forms as you wish as well and note that the forms WILL BE PREFIXED including the post data if you need to reach in there. However you can probably use the API and do what you are after just fine.
Can you describe what you want to do with both forms? I can give you a little better guidance as there is no documentation on this class just yet (coming soon).
The multi-instance has a few extra callbacks you can use as well as the normal ones (onSaving, onPosting, etc) that you can use to manipulate both forms.
Some examples in the wild:
Those are both added in the controller: https://github.com/anomalylabs/pages-module/blob/2.6/src/Http/Controller/Admin/PagesController.php#L68f
I delegate the adding of forms to the commands to clean up the controller but you can do this all inline or however you want. Feel free to ask some questions!