Announcement Announcement Module
Collapse
No announcement yet.
Problem binding boolean value to checkbox Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem binding boolean value to checkbox

    Hello everyone.

    I have a really annonying problem with a somewhat evident thing : setting a boolean value of a command object from a checkbox.
    Here is my code.

    Business Class
    Code:
    public class DemandeDeCatalogue {
        private boolean optin;
        private Address address = new Address();
        private String codeGestion = DemandeDeCatalogue.UNSPECIFIED_CATALOGUE;
        public static final String UNSPECIFIED_CATALOGUE = "-1";
    ...(snip)
    (note : getters and setters are ok, of course... )

    Controller Class
    Code:
    public class CatalogueDemandeFormController extends BaseFormController {  
        (snip...)
        public CatalogueDemandeFormController() {
            setCommandClass(DemandeDeCatalogue.class);
        }
        (snip...)
        protected void initBinder(HttpServletRequest request,
                ServletRequestDataBinder binder) {
            
            binder.registerCustomEditor(
                    null,
                    "address.dateNaissance",
                     new CustomDateEditor(AddressValidator.dateFormat, true)
            );
            
            binder.registerCustomEditor(
                    null,
                    "optin",
                     new CustomBooleanEditor(true)
            );
                (snip...)
        }
    and
    jsp form extract
    Code:
    <form action="demandeCatalogue.html" method="post" name="demandeCatalogueForm">
            <spring&#58;bind path="command.address.civility">
            <p><label for="<c&#58;out value="$&#123;status.expression&#125;" />">Civilité</label>
                <input type='radio' name='<c&#58;out value="$&#123;status.expression&#125;" />' value="1" <c&#58;if test="$&#123;status.value == 1&#125;">checked="checked"</c&#58;if> />Monsieur
                <input type='radio' name='<c&#58;out value="$&#123;status.expression&#125;" />' value="2" <c&#58;if test="$&#123;status.value == 2&#125;">checked="checked"</c&#58;if> />Madame
                <input type='radio' name='<c&#58;out value="$&#123;status.expression&#125;" />' value="3" <c&#58;if test="$&#123;status.value == 3&#125;">checked="checked"</c&#58;if> />Mademoiselle
                <input type='radio' name='<c&#58;out value="$&#123;status.expression&#125;" />' value="4" <c&#58;if test="$&#123;status.value == 4&#125;">checked="checked"</c&#58;if> />Société
            </p>
            </spring&#58;bind>
    
            <spring&#58;bind path="command.address.dateNaissance">
            <p><label for="<c&#58;out value="$&#123;status.expression&#125;" />">Date de naissance</label>
                <input type="text" 
                    name="<c&#58;out value="$&#123;status.expression&#125;" />"
                    id="<c&#58;out value="$&#123;status.expression&#125;" />"
                    value="<c&#58;out value="$&#123;status.value&#125;" />"
                    size="10" maxlength="10" />
            </p>
            </spring&#58;bind>
    
            <spring&#58;bind path="command.address.diversAdresse">
            <p><label for="<c&#58;out value="$&#123;status.expression&#125;" />">Divers adresse</label>
                <input type="text" 
                    name="<c&#58;out value="$&#123;status.expression&#125;" />"
                    id="<c&#58;out value="$&#123;status.expression&#125;" />"
                    value="<c&#58;out value="$&#123;status.value&#125;" />"
                    size="30" maxlength="30" />
            </p>
            </spring&#58;bind>
    
    
                <spring&#58;bind path="command.optin">
                <input type="checkbox" name="<c&#58;out value="$&#123;status.expression&#125;" />" id="<c&#58;out value="$&#123;status.expression&#125;" />" 
                <c&#58;if test="$&#123;status.value == 'on'&#125;">checked="checked"</c&#58;if> />
                </spring&#58;bind>
    
                <input type="submit" id="save" name="save" alt="Enregistrer" title="Envoyer" />            
    
    </form>
    I have and AddressValidator class that perform checks on the nested Address class included in the DemandeDeCatalogue class.

    Fact is that, after some tests, that EVERYTHING works fine (validating nested Address fields was easy as a breathe ! ) BUT the 'optin' fields fails miserably !
    I get a
    Code:
    Failed to convert property value of type &#91;java.lang.String&#93; to required type &#91;boolean&#93; for property 'optin'; nested exception is java.lang.IllegalArgumentException&#58; Invalid boolean value &#91;on&#93;
    when I check the box.

    I'm totally helpless... everything run smoothly without any problem, even the Date field (providing I set the CustomDateEditor), but it obviously seems to ignore my BooleanCustomEditor...

    Any help would be GREATLY appreciated !

    Thanks,

    sne.

  • #2
    Take a look at this post

    Comment


    • #3
      Well, I went through the proposed threads, and I must admit that I didn't found any really 'satisfying' solution...

      Adding a hidden field with a "_" is too "framework specific" for me. Using Spring (or whatever) shouldn't impose me to add html specific content on my pages... like the "[]" imposed in the name of multiple select in PHP.

      Furthermore, setting a 'value' to the checkbox to make it work is not satisfying.

      The role of the CustomBooleanEditor is clearly to handle the default value of a checked checkbox (see http://static.springframework.org/sp...ditor(boolean))
      which is 'on'.
      A value that here (see the error code) is rejected for a reason that goes beyond my understanding !

      Any idea ?

      Comment


      • #4
        anyone?

        Any Guru out there that can point me to a clean solution ?

        Thanks, I'm really stuck with that !

        Comment


        • #5
          Ok... just made an update of my version 1.1.3 to 1.1.5 and it's ok... was obviously a bug...

          Anyway, thanks for helping !

          Comment

          Working...
          X