You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Pierre Métras <ge...@sympatico.ca> on 2000/11/24 19:13:44 UTC

Validation of forms in version 1.0

Hi,

In version 1.0, validation of a form occurs only if the "input" attribute of
an action is set, in the struts-config.xml file (cf.
ActionServlet.processValidate).

Wouldn't it be more "logical" to do the validation, either the input form is
defined or not, and at the end of the function, iff errors in validation are
found, redirect to the input form if defined or return a server error if
not?

- As a default, validation of a form returns true. So always validating an
ActionForm is only a small performance problem, now that there no more
separation between validating and non validating forms.
- Without that change, multipart form data would be always accepted because
the rollback is never called, leaving uploaded files on the server...
- Same problem with Cancel action.


Another manual patch (ActionServlet.java):

    protected boolean processValidate(ActionMapping mapping,
        ActionForm formInstance, HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {

        if (formInstance == null)
            return (true);
        if (debug >= 1)
            log(" Validating input form properties");

// --------REMOVE FROM HERE ------------
        // Has an input form been specified for this mapping?
        String uri = mapping.getInput();
        if (uri == null) {
            if (debug >= 1)
                log("  No input form, no validation");
            return (true);
        }
//-------- TO THERE --------------------------

        ... Validation code...

        // Save our error messages and return to the input form if possible
//--------- REPLACE FROM HERE ---------
        String uri = mapping.getInput();
        if (uri == null) {
            if (debug >= 1)
                log("  Validation errors but no input form defined!");
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                   internal.getMessage("processValidate"));
        }
        else {
            if (debug >= 1)
                log("  Validation error(s), redirecting to: " + uri);
            request.setAttribute(Action.ERROR_KEY, errors);
            RequestDispatcher rd =
getServletContext().getRequestDispatcher(uri);
            rd.forward(request, response);
        }
//----------DOWN TO THERE------------------
        return (false);

    }


And you must define a new message:
processValidate=Can't display validation errors: no input form defined.


Pierre Métras