Figured out a nice Jakarta Struts 1.0.2 trick. If you are trying to do form validation, and you are storing your form bean in the session, you may get some validation errors from the validate method of your form bean. Struts may try to validate your form on a normal display if your user has visited the form, left, and then returned to the form.
To prevent the form bean from validating when the user has not submitted the form, I put a hidden input field in my HTML form called "submitted" and set its value to "true". In the validate method of my form bean, I put:
// Check to make sure that the form was submitted if (request.getParameter("submitted") == null) { log.debug("Form was not submitted, return no errors."); return null; }
This checks to see if the hidden input field was in the request when the form is displayed. If it wasn't, it means that the user didn't actually submit the form. So, the block returns null to tell Struts that there were no ActionErrors when validating the form.
When the user actually submits the form, the value of "submitted" will be "true" and the above block of code won't execute.
12:33:52 PM
|