Checkbox values in a form #139
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hey again,
I've just been checking through this with a form and I've noticed some strangeness with how forms are being handled (namely, checkboxes in forms)
The default behaviour for a browser is that on submitting a form, it will only include a checkbox if it is checked, and that the value of the checkbox is what is sent. I.e. you can attach the value attribute as what gets submitted, and it changes the value that gets submitted (default is "on")
However, after debugging it some more, I've realised that the parser for the form may be looking to see if the value attribute may be present and submitting that value, instead of checking first if it's checked.
To compare the results, I added a simple log below the paramObject parsing and discovered the difference (using FormData to get the data from the form)
As a result of this, I'd suggest changing the parser to using FormData, something like the following
For the moment I've hotpatched this as it's crucial for the application I've added in
Unfortunately,
FormData.entries()is a newer feature, so not all browsers support it.Hmm, any chance you can put together a test case using JSFiddle or similar? There's a form example under
example/forms.htmlyou can use as a base.The current method for building the request params object should only be including checked inputs though, as
if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) {results in all checkboxes and radio buttons except those that are checked being skipped.However, the PR that fixed various issues around form submission (#129) landed after 0.2.5 was released, so unless you're using the latest code from master, you won't have this in your copy of the library -- please can you confirm which version of Pjax you're using?
Yeah, just checked and the code sample you posted shows that you're using 0.2.5 (the same code block from master looks like this) -- checkout the library from master and I think you'll find everything should work as expected.
I'm using yarn, and the latest release is 0.2.5 on there
Try installing from the Github repo instead, so you get the latest commits.
With npm, that would be
npm install moox/pjax. Yarn probably has a similar command.That seems to have fixed it now, it was a bit of a pain to get though, when is 0.2.6 coming out? I usually keep all of my assets as the required version which is why I ask
When all the bugs are fixed. 😀