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

  • Submission of empty form fields

    I don't think form fields that are empty (0 length strings) should populate the form backing object. Currently I am getting empty string fields in my form rather then null. I don't know what you would get with empty non-string fields!

    Is their a way to change this behaviour or make it the default? Should I file this in jira somewhere?

    Cheers
    Steve

  • #2
    Steve,

    You said empty form field shouldn't populate the form backing object. But if I read further, what I think you are saying is that you think it shoudl populate it with nulls, not as an empty string? But why is one inherently more correct than another? In any case, at this point it's a bit moot, as changing this kind of behaviour would change backwards compatibility...

    Regards,

    Comment


    • #3
      I agree with Colin - it's perfectly reasonable to expect that submitted forms with empty fields may need to be seen as empty strings on the backing object. For case by case needs you can cause null to be set on the object in one of the onBindAndValidate methods I think.

      Comment


      • #4
        Originally posted by Colin Sampaleanu
        Steve,

        You said empty form field shouldn't populate the form backing object. But if I read further, what I think you are saying is that you think it shoudl populate it with nulls, not as an empty string? But why is one inherently more correct than another? In any case, at this point it's a bit moot, as changing this kind of behaviour would change backwards compatibility...

        Regards,
        I am sorry I was unclear. What I mean is that empty strings should be treated as nulls, I do not want empty strings populating my form object. The reason they are different is that they are different. I would need to make an extra test for every not-nullable field to make sure it is not-nullable AND not-empty. What is done in the case of, for example, a non-string object such as Integer? I imagine String fields are being treated differently then all others in this regard.

        My second wish would be that spring detected changed fields, and only populated the form object with user modified fields.

        I am certainly happy with treating empty strings as nulls as a configuration option (in order to preserve backwards compatibility).

        Comment


        • #5
          Originally posted by davison
          I agree with Colin - it's perfectly reasonable to expect that submitted forms with empty fields may need to be seen as empty strings on the backing object. For case by case needs you can cause null to be set on the object in one of the onBindAndValidate methods I think.
          I can certainly see the current behaviour as one reasonable approach (and the one taken obviously). However, current behaviour says that I supply a form object with null in a field, and get back a form object with an empty string in it's place even though the user did not change the field.

          Of course, you could argue that I could supply a form object with an empty string and get back null, but it seems to me a much less likely or harmful case. Hence, I would be happy with the behaviour being an option.

          Comment


          • #6
            Hey, I just discovered the StringTrimmerEditor, it looks like it already has the option to turn empty strings to null.

            Thanks!

            Comment


            • #7
              I'd suggest looking at Commons Lang, especially the StringUtils class. Become very familiar with it as it has become one of most useful classes I have used. It has a static method called trimToNullwhich will convert all empty strings to null for you. It has a ton of other methods which I have found to be invaluable in my programming. Best of all, no more NullPointerExceptions!

              Comment


              • #8
                Hiyaa!

                Originally posted by steven.warren
                Hey, I just discovered the StringTrimmerEditor, it looks like it already has the option to turn empty strings to null.

                Thanks!
                Terrific! Just saved me a days work. Thanks!

                Comment

                Working...
                X