Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Submitting empty field on form

  1. #1
    Join Date
    Nov 2004
    Location
    Jogjakarta, Indonesia
    Posts
    40

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

    Default

    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)

  3. #3
    Join Date
    Nov 2004
    Location
    Jogjakarta, Indonesia
    Posts
    40

    Default

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

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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.

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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);
        }

  6. #6
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

  7. #7
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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.

  8. #8
    Join Date
    Nov 2004
    Location
    Jogjakarta, Indonesia
    Posts
    40

    Default

    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.

  9. #9
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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

  10. #10
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Not updating a form field on setFormObject()?
    By SCWells72 in forum Swing
    Replies: 13
    Last Post: Aug 28th, 2005, 12:43 PM
  3. How to mark a form field as required???
    By Christian in forum Web
    Replies: 4
    Last Post: Sep 21st, 2004, 02:26 AM
  4. Replies: 10
    Last Post: Sep 3rd, 2004, 07:31 AM
  5. Submission of empty form fields
    By steven.warren in forum Web
    Replies: 7
    Last Post: Aug 17th, 2004, 03:36 AM

Posting Permissions

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