Results 1 to 3 of 3

Thread: Checkboxes with a false value

  1. #1
    Join Date
    Sep 2010
    Posts
    8

    Default Checkboxes with a false value

    I have a Roo generated jspx form with a checkbox as follows:
    <field:checkbox disableFormBinding="true" field="isCompleteTrue" id="f_com_novexinc_orderhawk_shrink_ShrinkMaster_i sCompleteTrue" z=""/>
    When the box is checked, the controller gets the request parameter as:
    &isCompleteTrue=on
    When it is unchecked, the parameter is missing, and I get:
    Required Boolean parameter 'isCompleteTrue' is not present

    How can I modify the @RequestParam("isCompleteTrue") Boolean isCompleteTrue,
    in the controller parameters to make this optional?

    Or, more simply put, should I not get:
    &isCompleteTrue=off
    when the checkbox is unchecked?

  2. #2
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    This is how your form is submitted by your browser unfortunately. Have you tried using a primitive boolean type for the field member. This should be defaulting to false in your controller.
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

  3. #3
    Join Date
    Sep 2010
    Posts
    8

    Default

    I eventually found the solution to this elsewhere. Using @RequestParam makes this parameter required.

    To handle optional parameters, you must do the following:
    Add HttpServletRequest req to the parameter list for your controller method, and then, inside the method:
    Code:
    Boolean isCompleteTrue = null;
    if (null != req.getParameter("isCompleteTrue"))
    {
            isCompleteTrue = new Boolean((req.getParameter("isCompleteTrue").equals("on")) || (req.getParameter("isCompleteTrue").equals("true")));
    }
    Last edited by Andrew Swan; Aug 7th, 2011 at 08:28 PM. Reason: Added code tags around code snippet

Tags for this Thread

Posting Permissions

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