This shoud do it. Isn't it?
private static PersonFormBean getPersonFormBean(Object bean, String property)
throws java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException,
java.lang.NoSuchMethodException{
return (PersonFormBean) BeanUtils.getProperty(bean, property);
}
On Tue, 11 Jan 2005 11:39:53 -0000, Daffin, Miles (Company IT)
<[EMAIL PROTECTED]> wrote:
> Dear All,
>
> I have a DynaValidatorActionForm like this:
>
> <form-beans>
> <form-bean name="theFormBean"
> type="org.apache.struts.validator.DynaValidatorActionForm">
> <form-property name="simpleProperty" type="java.lang.String" />
> <form-property name="personList" type="java.util.ArrayList"
> size="0" />
> <form-property name="newPerson"
> type="com.plok.validator.beans.PersonFormBean"/>
> </form-bean>
> </form-beans>
>
> When the related form is submitted the action adds the 'newPerson' to
> the 'personList' and replaces newPerson, on the DynaBean, with a new
> PersonFormBean. This all works fine. Now I want to add declarative
> validation for the newPerson, so only valid newPersons reach the action
> code that adds them to the list.
>
> The thing is that PersonFormBeans (in this example) have a start and end
> date (a period). The validation rules for these fields are:
> * start date and end date must be dates (dd/MM/yyyy)
> * start date must be before end date
> * end date must be after or equal to today
> * etc...
>
> So, you see, apart from the first rule, this goes beyond what can be
> achieved by plucking out the fields from the nested bean and using the
> default set of simple field validators shipped with struts (required,
> date etc.). (Even if you disagree that these validation requirements
> cannot be met in using the default validators please read on. This is
> just an example.)
>
> I have created a new validator that I want to handle the entire nested
> PersonFormBean: personValidator.
>
> <validator
> name="personValidator"
> classname="com.plok.validator.beans.PersonFormBeanValidator"
> method="validatePerson"
> methodParams="java.lang.Object,
> org.apache.commons.validator.ValidatorAction,
> org.apache.commons.validator.Field,
> org.apache.struts.action.ActionErrors,
> javax.servlet.http.HttpServletRequest"
> msg="errors.person">
> </validator>
>
> public static boolean validatePerson(Object bean, ValidatorAction va,
> Field field, ActionErrors errors, HttpServletRequest request)
> {
> PersonFormBean personFormBean =
> somehowGetTheBloodyPersonFormBeanOffTheBean(bean);
> // Do the validation directly on the personFormBean.
> return true;
> }
> private static PersonFormBean
> somehowGetTheBloodyPersonFormBeanOffTheBean(Object bean)
> {
> // How do I implement this?
> // It has to be generic, in case I change my mind about the type of
> the bean,
> // e.g. make it a simple bean with getters and setters instead of
> using the DynaBean...
> return null;
> }
>
> Would you:
> a) grab the nested PersonFormBean from the main, parent bean and then
> work directly with this? If so how? Is there a utility method somewhere?
> b) grab individual fields from the nested bean using BeanUtils methods
> (or other - feel free to advise).
>
> Your thoughts/urls would be appreciated.
>
> Many thanks.
>
> -Miles
>
> Miles Daffin
> Morgan Stanley
> 20 Cabot Square | Canary Wharf | London E14 4QA | UK
> Tel: +44 (0) 20 767 75119
> [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> --------------------------------------------------------
>
> NOTICE: If received in error, please destroy and notify sender. Sender does
> not waive confidentiality or privilege, and use is prohibited.
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]