Results 1 to 5 of 5

Thread: Problem binding boolean value to checkbox

  1. #1
    Join Date
    May 2005
    Posts
    4

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Take a look at this post

  3. #3
    Join Date
    May 2005
    Posts
    4

    Default

    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...tor(boole an))
    which is 'on'.
    A value that here (see the error code) is rejected for a reason that goes beyond my understanding !

    Any idea ?

  4. #4
    Join Date
    May 2005
    Posts
    4

    Default anyone?

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

    Thanks, I'm really stuck with that !

  5. #5
    Join Date
    May 2005
    Posts
    4

    Default

    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 !

Similar Threads

  1. how to Binding with checkbox
    By Rasheed in forum Web Flow
    Replies: 10
    Last Post: Apr 5th, 2011, 09:29 PM
  2. Replies: 8
    Last Post: Jul 27th, 2006, 02:47 AM
  3. Replies: 3
    Last Post: Sep 30th, 2005, 01:40 PM
  4. Replies: 1
    Last Post: Mar 4th, 2005, 06:13 PM
  5. Replies: 2
    Last Post: Aug 17th, 2004, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •