Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
In our Hanami project we've run into an issue when we use AJAX and the request is a success. If we want to re-render a section of the page without reloading or redirecting, any form that we attempt to re-render defaults to using the params for the values. At that point however, those params are outdated, and we don't want them to populate the form fields. This is particularly a problem when the params contain a collection, as the form builder is not well equipped to deal with them (it breaks when it attempts to use
We've talked around it a bit and come up with a couple of potential solutions, though we're not 100% happy with either of them. Thought I'd open it as an issue and get some feedback, as one solution involves editing the form builder.
The first option is to have something like this in any view with a
Using this method we get to retain the params object whilst emptying the part of it which the template actually uses.
The second method we played around with was to essentially add a
Finally, in the
Would love to hear some feedback on this. Thanks.
@DannySantos I just want to double check: is your goal to clear all the form values after AJAX submission? Would this fix your problem? https://www.w3schools.com/jsref/met_form_reset.asp
@jodosha No the goal isn't to clear all the values, it's to make sure that all of the values are correct, whether that means clearing them or, in the case of an update form, making sure they are populated by the attributes from the database and not the params. As our project has developed this has raised questions a few different times. Having just done a bit more hacking on this we've just realised that in the past we have had to get around this by specifying the values of individual fields. This also feels wrong, and like we're overwriting a piece of core Hanami functionality.
@DannySantos Let me rephrase. We're talking about invalid data in form submission that is happening via AJAX.
Is this correct?
@jodosha Sorry, I should have been more clear! We're not just talking about invalid data, but valid data too.
Which is called in the
This could possibly be an issue that needs to be raised separately as none of the solutions we have come up with directly address this problem.
So back to the original point, to get around this the two ideas we've come up with involve either manually (but not very cleanly) clearing the params in a view method, or adapting the
I hope that's a little clearer now.
@DannySantos Thanks for the well crafted explanation. For the part regarding the params, I thought that it would be feasible to fix your problem by allowing to override params inline.
Would you please check if this patch fixes your params problem? #150
The PR contains an example to use the enhancement.
To try the PR, edit your
gem "hanami-helpers", git: "https://github.com/hanami/helpers.git", branch: "enhancement/form-for-inline-params"
$ bundle update hanami-helpers