Announcement Announcement Module
Collapse
No announcement yet.
Submitting empty field on form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Submitting empty field on form

    How would I allow some empty field on form submission? What do I need to add on my controller? Because the form would only insert data if all fields weren't empty.

    Thanks in advance

  • #2
    How would I allow some empty field on form submission?
    Depends what the field is. AFAIK, if it's a primitive you can't if your binding to it. Otherwise you can define it in your property editor. For example, CustomDateEditor has the signature:
    Code:
    CustomDateEditor(DateFormat dateFormat, boolean allowEmpty)

    Comment


    • #3
      So it would be better to get the value with request.getParameter() rather than binding it?

      Comment


      • #4
        All HTML input types *except for checkboxes* will return an empty string. Is this what you mean?

        To get around the horrific flaw in checkbox handling, you can have a hidden element with "_propertyName" in your form. When spring binds it will intialise to the default values *including primitives* any properties which have a corresponding "_propertyName" parameter.

        HTH.

        Comment


        • #5
          Code:
          To get around the horrific flaw in checkbox handling
          I'm sure yatesco means horrific handling by browsers

          This is a problem for all server side handling of checkboxes. You can also override onBindAndValidate:
          Code:
              protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws java.lang.Exception {
                  MyObject myObject = (MyObject) command;
                  myObject.setLikesSpring(request.getParameter("likesSpringCheckBox") != null);
              }

          Comment


          • #6
            So it would be better to get the value with request.getParameter() rather than binding it?
            Probably not if it is an object. I guess your other option is to set the allowFields property accordingly, and do the binding yourself - a bit messy though.

            Comment


            • #7
              yes, I did mean in browsers

              strbuf; I am not sure I get your problem, are you asking whether a form can be partially populated, i.e. only with parameters that are sent?

              For the intellectually challenged amongst us (i.e. me ) can you explain the problem, and the solution you want.

              Comment


              • #8
                Say I have two input fields.

                input type="text" name="name"
                and
                input type="text" name="address"

                I want the form still submitted/insert the data eventhough the address field is not filled in/empty. Using spring:bind won't allow me to have empty fields on form submission.

                Comment


                • #9
                  Why? What exception do you get?

                  assume:
                  Code:
                    class MyForm {
                      private String address;
                      private String name;
                  
                      //accessors hidden.
                    }
                  you can certainly bind on the properties of that form regardless of whether they are null

                  Comment


                  • #10
                    Using spring:bind won't allow me to have empty fields on form submission
                    This should work. You might have to post the relevant code to get this sorted out.

                    Can I suggest first looking at the sample applications in the Spring distribution, and try submitting empty fields yourself.

                    Comment


                    • #11
                      Now I know what the problem is. Date fields can not be empty.

                      I get this message:
                      Code:
                      Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'tanggalLahir'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: ""
                      Would there be any way to let null value date be inserted?

                      Comment


                      • #12
                        Date fields can not be empty.
                        Did you specify allowEmpty=true when redistering CustomDateEditor(DateFormat dateFormat, boolean allowEmpty)

                        Comment

                        Working...
                        X