Results 1 to 5 of 5

Thread: Clear form field after failed validation

  1. #1
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default Clear form field after failed validation

    This seems like it would be such an obviously trivial thing to do but for the life of me I can't figure out how.

    I've also searched the forums and google for a way to do this for at least an hour and have still come up empty.

    The scenario: I have a form with a captcha image. In the case of an invalid form submission (validation fails) the user will be directed back to the original form. However, since the captcha image is regenerated on every request, I want to clear the previous value the user entered in this form field.

    I tried an obvious approach and tried to null out the previously entered user value in the form backing object that was passed into the validator. However this didn't work.

    Clearly, spring is repopulating the form with values held somewhere else than in the form backing object that is passed into the validator(s).

    How do I clear a previously entered form value during form submission so that the field is empty when the user is represented the form?

  2. #2
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    Ok, so I have more information now.

    It turns out that if I clear the model field in the referenceData() method and the validator didn't reject that field, then the field does indeed get cleared.

    However, if you specifically reject a field, the tag library ignores the null value in the model and uses the cached value in the FieldError object.

    In my case, there's no point in showing the user what they entered because the captcha will have changed and the old value will never be anywhere close to reusable.

    Anyone know of any way to override this so that the input tag does NOT use the rejected value?

  3. #3
    Join Date
    May 2007
    Posts
    17

    Post Clear the field before rejecting the value.

    Clear the field BEFORE rejecting the value. Of course then you can't use some of the handy all-in-one utility functions. But....

    Ex.

    if (yourBackingBean.getSomeField().equals("<some invalid value>" )
    {
    yourBackingBean.setSomeField("");
    errors.rejectValue("someField","your.message.key") ;
    }

  4. #4
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    Quote Originally Posted by sams_6 View Post
    Clear the field BEFORE rejecting the value. Of course then you can't use some of the handy all-in-one utility functions. But....

    Ex.

    if (yourBackingBean.getSomeField().equals("<some invalid value>" )
    {
    yourBackingBean.setSomeField("");
    errors.rejectValue("someField","your.message.key") ;
    }
    LOL, thanks! Sometimes the solution is right in front of you and you can't see it....

  5. #5

    Default

    Grt Solution...worked like charm...

Posting Permissions

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