Announcement Announcement Module
Collapse
No announcement yet.
Binding Composite Date Fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding Composite Date Fields

    I have a form with 3 select boxes (day, month & year). I need to bind these fields to a single Date field in my command object. What is the best practice for doing this?

  • #2
    You can make your own date form object with each field (day, month & year) as a nested property. This allows you to valid the fields individually or as a group.

    Another option would be to combine them into a single string in Javascript, and submit that.

    Comment


    • #3
      Or make use of the deprecated methods (shock horror ) of the existing java.util.Date (get/SetDay|Month|Year).

      Comment


      • #4
        i have this broblem and, if possible, i would like to see, please, some example of solution... i have the following:
        Code:
        <Spring:bind path="fxRate.rpDate">
         <input type="text" name="rpDate" size="10" value="${status.value}" />
        </Spring:bind>
        the date is shown, like yyyy-mm-dd... without any problem, but when this field is modificated, the onSubmid method is not called because, i think, of some validation of spring, that cannot "translate" this new yyyy-mm-dd in Date again... can u plz help me with that?
        thks a lot

        Comment


        • #5
          Have you registered a property editor against your date field?

          Comment


          • #6
            if possible, i would like to see, please, some example of solution
            From AbstractClinicForm in the PetClinic example:
            Code:
                protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    dateFormat.setLenient(false);
                    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
                    binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
                }

            Comment


            • #7
              thks for replies!!

              Originally posted by yatesco
              Have you registered a property editor against your date field?
              yes :o

              Originally posted by katentim
              From AbstractClinicForm in the PetClinic example:
              Code:
                 
              protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
                      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                      dateFormat.setLenient(false);
                      binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
                      binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
              }
              not working :_-(

              Code:
              public void initBinder(HttpServletRequest req,ServletRequestDataBinder binder) throws Exception{
               SimpleDateFormat specFormat = new SimpleDateFormat("yyyy-MM-dd");
               CustomDateEditor specEditor = new CustomDateEditor(specFormat, true);
               binder.registerCustomEditor(Date.class, "rpDate", specEditor);
              }
              this one too..

              may be is there a problem with java.sql.Date class? do i need to use java.util.Date ??
              when i just do not show this field (it is pre-setted earlier in formBackingObject() method), all works perfecly! (but SOMETIMES this pre-setted date needs to be modified)
              plz help :o my chef is starting to look eagly at me about this problem... thks again!
              Last edited by orusso; Jun 28th, 2006, 05:11 AM.

              Comment


              • #8
                do i need to use java.util.Date ?
                Yes. CustomDateEditor is a property editor for java.util.Date.

                Comment


                • #9
                  Originally posted by katentim
                  Yes. CustomDateEditor is a property editor for java.util.Date.
                  but all java.sql.Date is java.util.Date... why it was not working? ( now it is :-D, i have modified the field extencion in POJO )
                  but now on ViewAll page, the date appear like "2006-06-28 00:00:00.0" .... with java.sql.Date it was "2006-06-28", a little bit more user-view-friendly... there is no initBinder() for the normal Controller interface (my viewAll-controller implements Controller)... do u plz have any suggestion for me? thks
                  Last edited by orusso; Jun 30th, 2006, 05:43 AM.

                  Comment


                  • #10
                    Originally posted by katentim View Post
                    You can make your own date form object with each field (day, month & year) as a nested property. This allows you to valid the fields individually or as a group.
                    I have the same issue, 3 fields: month, day, year and wanting to combine them into one date. I like the above solution, but I don't know if it really solves the original problem of how to bind 3 fields into one java.util.Date object. Any suggestions?

                    Thanks,
                    Tim

                    Comment


                    • #11
                      Hi All,
                      binder.registerCustomEditor(Date.class, "rpDate", specEditor);
                      wouldn't it be a good thing if the registerCustomEditor takes an array of fields and let the CustomEditor prepare a Date.class of the given fields .

                      Thanks
                      Sami

                      Comment


                      • #12
                        You can leave out the property path parameter ("rpDate" in your sample), i.e. passing null, and the property editor will be applied to all of your Date fields. This is not exactly what you describe, but might be sufficient for your use case.

                        Jörg

                        Comment


                        • #13
                          Have the same thing in setting up a date property for birthday. Also have a problem in binding new Date() to <form:hidden path="formDate" id="formDate" /> . Any solutions? I tried js, but still have a problem with it.

                          Comment

                          Working...
                          X