-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hx-params is ignored if included inside a form element #3199
Comments
Based on the testing suite (hx-params: named includes works), this shouldn't be happening. Can you post your actual code, perhaps we can find the issue somewhere else? |
I have the same issue. It doesn't even have to be an field in the form. Forms in general seems to ignore the hx-params attribute. Call "localhost/?some_fancy_param=1"
Submit the form and you will get "localhost/?field1=&field2=&field3=", but some_fancy_param is gone for ever. It's working fine on e.g. selects EDIT: After some further testing I also had problems on "a"-tags |
A possible workaround (works only for GET requests and does not take data from any fields):
Maybe you can reuse some of the code to fix the issue... |
hx-params is ignored. My workaround was to enclose all fields inside a div and make the hx-post from the div. That way hx-params worked like intented. |
Tested your pasted form code and it works as expected in this example codepen: https://codepen.io/MichaelWest22/pen/yyLewZx Your form only includes the specified input field names but excludes the last one I added with a different name. I tested with multiple old versions of htmx as well and they all seemed to work for me as expected |
I think this is expected behavior. The current query parameters of a site are never designed to be submitted with every future form request inside the page as this would be a major security issue if this was possible. Someone sending you to a link to the page could inject malicious parameter data into your next form submission to get you to do all sorts of dangerous things! Use this link to pay me $1 please: https://yourbank.com/make-payment?payment-amount=1000 :) There are much better more secure patterns I think to handle this situation if you did happen to need a url query parameter to be reused by some of the forms on the page. For example when loading a page with such a parameter that you know is safe and needed for the next form submission you could create a hidden input in the form with the previous url value on page load. or in htmx if you were doing a partial page update and pushing a url with a parameter you could retain an input on the page with the value so it will be submitted next time. Also you can use the configRequest event like you have in your example above but you should only need 3 lines as you can leave htmx to generate the parameters list as normal which it does before it calls configRequest and then your event listener code can just optionally add one extra parameter into event.details.parameters if it is missing or required from the expected and knows to be safe current url query parameter. |
Valid point, still I think this should be secured another way... In my case I just use it for some sorting and filtering. A option to disable this protection would be great. The goal should not be that dev are searching them selfs workarounds. |
I agree with @MichaelWest22, but I think you can get your desired behavior by setting |
By the default, if the HTMX request is sent from a form element, all fields are sent and hx-params is ignored.
Maybe the documentation should be updated to mention this behaviour or change the behaviour to allow filtering parameters even if the request is sent from a form element.
In the previous example, all the fields will be sent ignoring hx-params. I would have thought that only 'field1' would be sent.
The text was updated successfully, but these errors were encountered: