On Mon, 2002-03-04 at 19:20, Matt Read wrote:

    I can see a solution where in validation() methods I check that the session
    is still valid but this seems clumsy to me. Is it good MVC design for the
    request to be handled by the Model before the Controller gets to see it? Am
    I incorrectly putting business logic into the validation() method when it
    should be dealt with further down the chain? Or should I be handling my
    authentication and session management in a subclass of the ActionServlet?
    
    I'd appreciate any insights.
    Matt.
    

You're putting too much business logic in the form's validate() method.
Struts best-practices dictacte that "form validation" should really be
thought of as a two-phase process.

Phase 1 should check for basic form correctness (e.g. did they really
put a valid number in the text field meant for a number? Is this a valid
date? Did they remember select a radio button value?). This phase should
be handled by the form bean's validate() method.

Phase 2 should be put into your Actions themselves, and should be the
business logic validation (e.g. is the user logged in? Is the date they
entered a valid date as far as the database of available dates is
concerned? Is the name they entered a valid name as far as the database
is concerned?).

As a side note - the phase 1 validation (calling the validate() method)
doesn't have to be done automatically by Struts. You can set
"validate=false" in your Action definition (struts-config.xml), and call
validate() manually from within your action if you choose. This is
useful when sometimes you want validation, sometimes you don't. For
example I often have one Action which handles record creation as well as
editing. Often I want validation in the create use-case, but not in the
edit use-case, and so I manually call validate() from the action rather
than have Struts do it automatically.

Bryan

Reply via email to