form-to-object
form-to-object copied to clipboard
wrong format single checkbox
Hi, Thanks for your job, that's help me a lot with my symfony project but I have an issue with the checkbox element. You return an array even if it's a single checkbox instead of return the value. Maybe I'm wrong but I guess it's an array if there are many checkbox but just a value if it's a single one.
IMHO this is not a great idea. If or when you redesign the form to include more than 1 checkbox, then you have to re-write the code that uses the JSON output.
Best to have the format of arrays to allow easy expansion. I do understand you wanting to reduce the input size. :)
I don't understand what you mean. I have many checkbox in my form, some of them are single and others works together like:
<input type="checkbox" name="single" value="value1">
<input type="checkbox" name="many[]" value="value2">
<input type="checkbox" name="many[]" value="value3">
And the result has to be a value for the input "single" and an array for the inputs "many[]" when the form is submitted, right? I have made a pull request for my change, I use it and everythings works fines.... in Symfony :) Where am I wrong?
I'm trying to have a decision based on some case studies and unit tests. I'll get back with a solution. Thank you for the feedback!
I don't understand what you mean.
If you have the check box result as a single value in a JSON 'object', then you will write a program to read it as a value. If later, you decide to make that check box as an 'array' of choices, then you have to edit your program to read as an array, not single value.
It is best to prevent the option of single-value vs (single) array; less re-writing of code.
@tomByrer if you decide to make your checkbox as an array of choices, you have to change the name of the input and add "[]" at the end.
And normally, if you use a framework or a cms, everything shoud work perfectly when you submit your parameters in the form. But if you're not and write your code by yourself, yes you have to make some modifications in your program because that's how PHP works.
http://www.html-form-guide.com/php-form/php-form-checkbox.html
But if you're not and write your code by yourself
Yea, I think I have a different view since I'm more or less building my own CMS (reading JSON from forms & displaying it via Handlebars or the like).
Just started to use this for form submission using AJAX and noticed that single checkboxes handle as an array which led me to this issue and discussion.
Justification for not submitting single checkboxes as an array is simply that any Javascript routine should mimic the browsers native HTML POST method. In the case of single checkboxes, the native method doesn't convert it to an array. If you have multiple checkboxes with a single name then the native method simply passes the last set value - no "arraification" at all.
The reason for this is that if you cannot assume the client is using a JS enabled browser in 100% of cases - I'm looking at accessibility browsers or some security driven industries. The non-JS fallback in those handful of cases will be the browsers own native method. By changing it to an array the server side code needs to handle both singular and array passed values.
Fixed in https://github.com/serbanghita/form-to-object/releases/tag/3.0.0-rc.0
Only took about ~10 yrs, but hey, it's fixed now 🥇 🙇 🤣