form2js icon indicating copy to clipboard operation
form2js copied to clipboard

Radio button true and false value bug

Open andrewchernow opened this issue 11 years ago • 1 comments

When a group of radios uses "true" or "false" for their values, form2js converts these to boolean true and false. However, if you add a 3rd radio to the group to indicate "none selected" or "no answer" or "any" and set its value to "", form2js will convert the checked radio with a value of "" to boolean false. However, if you don't use "true" and "false" as the values, the "" value works as expected.

This is because of the radio case in getFieldValue, which falls-through to the checkbox case. The offending line from the checkbox case is:

if(!fieldNode.checked && fieldNode.value === "true") return false;

This is correct for checkboxes, but for something like:

any on off

It is broken. I should not get back boolean false if the "any" radio is checked. I should get back "". To fix this, i just copied the statements from the checkbox case in getFieldValue, excluding the offending line, to the radio case.

Point is, this feels like a bug or maybe just unexpected behavior. IMHO, getFieldValue should not treat a radio just like a checkbox.

andrewchernow avatar Aug 13 '14 18:08 andrewchernow

Sorry, I didn't format the radio group.

<input type="radio" name="state" value="" checked="checked" />any <input type="radio" name="state" value="true" />on <input type="radio" name="state" value="false" " />off

The above produces:

{"state": false}

What I would expect and what the change I made produces is:

{"state": ""}

andrewchernow avatar Aug 13 '14 18:08 andrewchernow