On Mon, 8 Jul 2002, Joseph Barefoot wrote:
> Date: Mon, 8 Jul 2002 17:21:32 -0700
> From: Joseph Barefoot <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>,
> [EMAIL PROTECTED]
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: RE: DynaActionForm Advantages
>
> hmmm...I'm sure this bespeaks my ignorance of DynaActionForm beans, but how
> would one implement custom validation (say to validate date or other field
> ranges) using DynaActionForm beans, since their specifications are in your
> configuration file?
>
As with doing a validate method for a standard ActionForm bean, you need
to know the name of the property you want to edit, as well as what rules
you want to apply. So, the basic idea would be something like this:
* Create a DynaActionForm subclass to contain your validate() method --
let's call it com.mycompany.MyForm and fill in the details later.
* Configure this bean in struts-config.xml:
<form-bean name="myform" type="com.mycompany.MyForm" dynamic="true">
...
<form-property name="startDate" type="java.lang.String"/>
...
</form-bean>
* Write your validate() method something like this:
pacakge com.mycompany;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.DynaActionForm;
public class MyForm extends DynaActionForm {
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
String startDate = get("startDate");
... perform the usual validations on this String ...
}
}
To retrieve property values, the key is that DynaActionForm implements the
DynaBean interface (from commons-beanutils), so it provides get() method
variants for simple property access, as well as indexed and mapped access.
There are corresponding set() methods to set the properties.
Alternatively, you can use the PropertyUtils methods on either kind of
bean transparently, and replace the getter above with:
String startDate = PropertyUtils.getProperty(this, "startDate");
It's also worth checking out the Validator Framework's support for
DynaActionForm beans, to see if it can do what you need without having to
write these kinds of validate() methods yourself.
> peace,
> Joe Barefoot
>
Craig
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>