PDA

View Full Version : Clear form field after failed validation


chudak
Mar 5th, 2008, 12:32 PM
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?

chudak
Mar 5th, 2008, 02:59 PM
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?

sams_6
Mar 5th, 2008, 03:49 PM
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");
}

chudak
Mar 5th, 2008, 04:22 PM
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....