Spring Framework
  1. Spring Framework
  2. SPR-5354

Binding doesn't work when no radio button in a set is selected

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Waiting for Triage
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Per http://static.springframework.org/spring/docs/2.5.x/reference/view.html#view-jsp-formtaglib-checkboxtag

      "What you might not expect to see is the additional hidden field after each checkbox. When a checkbox in an HTML page is not checked, its value will not be sent to the server as part of the HTTP request parameters once the form is submitted, so we need a workaround for this quirk in HTML in order for Spring form data binding to work. The checkbox tag follows the existing Spring convention of including a hidden parameter prefixed by an underscore ("_") for each checkbox. By doing this, you are effectively telling Spring that " the checkbox was visible in the form and I want my object to which the form data will be bound to reflect the state of the checkbox no matter what "."

      In HTML, radio buttons behave in the same way - if none of them are selected, the server gets nothing. But unlike checkboxes, Spring isn't adding this workaround for radio buttons.

        Activity

        Hide
        Stevo Slavić added a comment -

        AFAICT no radio button from radio button group being selected can result from three cases:

        • when no default value is defined for the property that radio buttons are bound to
          • in this case 'field was present' marker wouldn't change anything, as there is no change in selection
        • radio buttons were bound to a property with a value from set of values assigned to radio buttons, and one of the radio buttons was checked, but using custom javascript user was enabled to remove selection so that no radio button is selected
          • in this case 'field was present' marker would help or better to say wouldn't harm, but I guess since this is no longer case of standard HTML component behaviour (it's changed via custom javascript), the way it's handled is OK - since developer added custom behaviour to the component it is OK to expect that developer is responsible to add generating 'field was present' marker as well if needed, while Spring will do its part, WebDataBinder will check for field markers
        • radio buttons were bound to a property with a value different from values assigned to radio buttons
          • this is most likely coding bug, and behaviour is somewhat equivalent to the first case

        Checkboxes and select element are different - these components by design allow removing selection, but because of "quirk in HTML" Spring designers had to apply 'field was present' marker workaround for these components only to be able to update form model accordingly with the selection change.

        IMO this is not a bug, works as designed.

        Show
        Stevo Slavić added a comment - AFAICT no radio button from radio button group being selected can result from three cases: when no default value is defined for the property that radio buttons are bound to in this case 'field was present' marker wouldn't change anything, as there is no change in selection radio buttons were bound to a property with a value from set of values assigned to radio buttons, and one of the radio buttons was checked, but using custom javascript user was enabled to remove selection so that no radio button is selected in this case 'field was present' marker would help or better to say wouldn't harm, but I guess since this is no longer case of standard HTML component behaviour (it's changed via custom javascript), the way it's handled is OK - since developer added custom behaviour to the component it is OK to expect that developer is responsible to add generating 'field was present' marker as well if needed, while Spring will do its part, WebDataBinder will check for field markers radio buttons were bound to a property with a value different from values assigned to radio buttons this is most likely coding bug, and behaviour is somewhat equivalent to the first case Checkboxes and select element are different - these components by design allow removing selection, but because of "quirk in HTML" Spring designers had to apply 'field was present' marker workaround for these components only to be able to update form model accordingly with the selection change. IMO this is not a bug, works as designed.
        Hide
        Jason Barnabe added a comment -

        I made this ticket over 3 years ago and have long gone past using Spring to generate form fields, but if I remember correctly, your first case was what was happening.

        I believe the problem was that I had a "required" validation on the field (i.e. the user had to pick a value). Since no value was sent to the server, the validation was not occurring as Spring didn't know the field was on the form. Adding the hidden underscored parameter would make Spring realize that the field was there and fire my validation.

        So both checkboxes and radio buttons have the same problem - lack of selection essentially makes the field invisible to Spring's binding. The only difference is that an unchecked checkbox means false while an unselected set of radio buttons means null. But in both cases the binding should be fired to allow validation to occur.

        Show
        Jason Barnabe added a comment - I made this ticket over 3 years ago and have long gone past using Spring to generate form fields, but if I remember correctly, your first case was what was happening. I believe the problem was that I had a "required" validation on the field (i.e. the user had to pick a value). Since no value was sent to the server, the validation was not occurring as Spring didn't know the field was on the form. Adding the hidden underscored parameter would make Spring realize that the field was there and fire my validation. So both checkboxes and radio buttons have the same problem - lack of selection essentially makes the field invisible to Spring's binding. The only difference is that an unchecked checkbox means false while an unselected set of radio buttons means null. But in both cases the binding should be fired to allow validation to occur.
        Hide
        Stevo Slavić added a comment -

        I'm not convinced, and I doubt Spring developers will be, that this is a bug because:

        • it's not documented that marker will be generated for radiobutton(s), and
        • bug in user code (lack of client side validation, validation misconfigured and not being triggered when parameter is not present in request, or validation not handling well null value for a bound field) is not Spring bug.

        This issue could be changed to improvement request. Although trivial from effort perspective, I doubt just adding always 'field was present' marker for form:radiobutton/radiobuttons tags will be accepted, as it was obviously left out by design decision.

        IMO more chance of being accepted has improvement request to:

        • extend form:radiobutton/radiobuttons} tags with an attribute (e.g. {{generateMarker) to let developers specify whether marker should be generated or not;
          • generateMarker should default to false, to continue by default not to generate marker for radio button(s), for backwards compatibility and consistency with original design decision;
          • when generateMarker is true and radio button is not disabled then generate marker
          • when generateMarker is true and radio button is disabled, then do not generate marker (consistent with form:checkbox tag logic)
        Show
        Stevo Slavić added a comment - I'm not convinced, and I doubt Spring developers will be, that this is a bug because: it's not documented that marker will be generated for radiobutton(s), and bug in user code (lack of client side validation, validation misconfigured and not being triggered when parameter is not present in request, or validation not handling well null value for a bound field) is not Spring bug. This issue could be changed to improvement request. Although trivial from effort perspective, I doubt just adding always 'field was present' marker for form:radiobutton/radiobuttons tags will be accepted, as it was obviously left out by design decision. IMO more chance of being accepted has improvement request to: extend form:radiobutton/radiobuttons} tags with an attribute (e.g. {{generateMarker ) to let developers specify whether marker should be generated or not; generateMarker should default to false, to continue by default not to generate marker for radio button(s), for backwards compatibility and consistency with original design decision; when generateMarker is true and radio button is not disabled then generate marker when generateMarker is true and radio button is disabled, then do not generate marker (consistent with form:checkbox tag logic)
        Hide
        Jason Barnabe added a comment -

        -"it's not documented that marker will be generated for radiobutton(s)" - I'm trying to wrap my head around this logic but I'm not able to. The Spring documentation is not a specification that the implementation needs to follow; it's a description of the implementation. The documentation can change.
        -"lack of client side validation" - you can't trust the client to do validation. If you could, then you wouldn't need Spring validation at all.
        -"validation misconfigured and not being triggered when parameter is not present in request" - this is the default behaviour of Spring validations, AFAIK. If you have a validator for "firstName" and there is no "firstName" field, your validation will not fire.
        -"validation not handling well null value for a bound field" - it's not the case that my validation was being given a null value; it's that my validation was not fired at all.

        I doubt that this was a conscious decision on the Spring developers' part. More likely it was an oversight due to the fact that in the majority of cases radio buttons start with one option selected or get one selected by the user. Regardless, I understand that this may pose a backwards compatibility issue and may need to be implemented by way of an additional attribute as you suggest.

        Show
        Jason Barnabe added a comment - -"it's not documented that marker will be generated for radiobutton(s)" - I'm trying to wrap my head around this logic but I'm not able to. The Spring documentation is not a specification that the implementation needs to follow; it's a description of the implementation. The documentation can change. -"lack of client side validation" - you can't trust the client to do validation. If you could, then you wouldn't need Spring validation at all. -"validation misconfigured and not being triggered when parameter is not present in request" - this is the default behaviour of Spring validations, AFAIK. If you have a validator for "firstName" and there is no "firstName" field, your validation will not fire. -"validation not handling well null value for a bound field" - it's not the case that my validation was being given a null value; it's that my validation was not fired at all. I doubt that this was a conscious decision on the Spring developers' part. More likely it was an oversight due to the fact that in the majority of cases radio buttons start with one option selected or get one selected by the user. Regardless, I understand that this may pose a backwards compatibility issue and may need to be implemented by way of an additional attribute as you suggest.
        Hide
        Doug Breaux added a comment -

        Adding link to a discussion in the forums that is at least related:

        http://forum.springsource.org/showthread.php?60504-Binding-on-missing-field-if-no-parameter-passed-back-the-quot-required-quot-mark-ignored

        I won't comment on whether this is the right solution, but it seems to me that it's definitely a bug that Web Flow's binding required="true" does not fail if no radiobutton value at all was selected.

        Show
        Doug Breaux added a comment - Adding link to a discussion in the forums that is at least related: http://forum.springsource.org/showthread.php?60504-Binding-on-missing-field-if-no-parameter-passed-back-the-quot-required-quot-mark-ignored I won't comment on whether this is the right solution, but it seems to me that it's definitely a bug that Web Flow's binding required="true" does not fail if no radiobutton value at all was selected.

          People

          • Assignee:
            Rossen Stoyanchev
            Reporter:
            Jason Barnabe
            Last updater:
            Chris Beams
          • Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since last comment:
              1 year, 14 weeks ago