Have you seen our video tutorials? Check 'em out!

Frontend ajax streams forms

samharrison7 - 2 months ago

I'm trying to create an ajax form (in a modal) for a stream on the frontend and am having some problems. If I set the $ajax field to true in my FormBuilder, then the action URL is just set to my base URL, but if I set it to false and build the ajax call myself, I'm not sure what URL to post to. Posting to the default form URL returns a full HTML page (with success/error messages set), but in an ideal case a JSON response with success/failure status would returned. Is there any magic URL that will return that?

Cheers, Sam

ryanthompson - 2 months ago

That's what it's supposed to do when $ajax is set to true. But there is some work to be done.

You can though, handle it yourself with your own URL and what not. You would want to compile the JSON response yourself. If you look at https://github.com/anomalylabs/streams-platform/blob/master/src/Http/Controller/FormController.php you can get an idea of what the default form handle does for front end forms.

You can use this same process and just return your own JSON response like return return $this->response->json($data); which is Laravel.

samharrison7 - 2 months ago

Thanks @ryanthompson, using a similar controller to the public FormController worked well. I just setAjax(true) on the builder and getFormResponse() was nicely JSONified with errors included.

ryanthompson - 2 months ago

Sounds good! Eventually ill get around to including it all in the Ajax response. Would you mind making a GitHub issue with everything you think should be in there?

samharrison7 - 2 months ago

Done :)