struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daffin, Miles (Company IT)" <Miles.Daf...@morganstanley.com>
Subject Validating DynaBean nested beans with a custom validator.
Date Tue, 11 Jan 2005 11:39:53 GMT
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
miles.daffin@morganstanley.com <mailto:miles.daffin@morganstanley.com> 
--------------------------------------------------------
 
NOTICE: If received in error, please destroy and notify sender.  Sender does not waive confidentiality
or privilege, and use is prohibited. 
 

Mime
  • Unnamed multipart/alternative (inline, 7-Bit, 0 bytes)
View raw message