You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Aaron Robinson <aa...@hotmail.com> on 2003/06/10 15:55:37 UTC

Reasonable to use EJBs for validation in ActionForm validate method?

For validation that can only be done against the database, should this go in 
the validate method or in the execute method?

For example checking that a customer doesn't alerady exist before inserting 
it, or checking that a user has entered a valid address before the execute 
method is called to try and create it.

Also, with the validator framework, does this just apply to basic (non model 
based) validation?

_________________________________________________________________
Find a cheaper internet access deal - choose one to suit you. 
http://www.msn.co.uk/internetaccess


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Reasonable to use EJBs for validation in ActionForm validate method?

Posted by Erik Price <ep...@ptc.com>.

Aaron Robinson wrote:
> For validation that can only be done against the database, should this 
> go in the validate method or in the execute method?
> 
> For example checking that a customer doesn't alerady exist before 
> inserting it, or checking that a user has entered a valid address before 
> the execute method is called to try and create it.

Obviously you can do it however you like, but the advice that was given 
to me is that validate() is not the best place to perform business-logic 
validation.  Rather, you probably already have a means of validating 
this data in your business logic, so you should wait until the data gets 
to that point and let it be handled there.

For instance, in your Action you validate the customer information to 
make sure that everything is syntactically valid, but you still have no 
idea if you are allowed to insert the customer.  So you pass the 
information to the business logic (model layer) and hope for the best. 
In your model you may have written a constraint in the DB that prevents 
the customer from being inserted if it already exists, in which case a 
SQLException is thrown, caught by your model, and perhaps re-thrown.  In 
this case your Action must be prepared to handle the exception.  Or if 
you are not using database constraints you might perform your own query 
to see if the customer already exists, and if so, throw some exception. 
  Either way, the Action doesn't really know what makes the data 
"valid".  This lets your model layer be more flexible.

As an addendum you might find it helpful to wrap the SQLException (or 
whatever) with some custom exception like MyAppNameInvalidDataException, 
so that your Action is further insulated from changes made in the model.



Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Reasonable to use EJBs for validation in ActionForm validate method?

Posted by Keith Pemberton <kb...@cox.net>.
The validation code of if the custom is already in the database should
be in your create method on the entity EJB.  Other validation code
concerning the webform (such as valid address) should go in the validate
method on the ActionForm that you have specified for the webform.  Hope
this helps.  Let me know if you need code samples.

Keith

On Tue, 2003-06-10 at 08:55, Aaron Robinson wrote:
> For validation that can only be done against the database, should this go in 
> the validate method or in the execute method?
> 
> For example checking that a customer doesn't alerady exist before inserting 
> it, or checking that a user has entered a valid address before the execute 
> method is called to try and create it.
> 
> Also, with the validator framework, does this just apply to basic (non model 
> based) validation?
> 
> _________________________________________________________________
> Find a cheaper internet access deal - choose one to suit you. 
> http://www.msn.co.uk/internetaccess
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
-- 
Keith Pemberton <kb...@cox.net>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org