| Author |
Validate over a collection of beans inside a bean.
|
Jimmy Chuck
Greenhorn
Joined: Feb 23, 2007
Posts: 2
|
|
Im about to go insane! I have a ValidatorForm called UserDetailsForm ... within that form I have two bean UserAccountDetailsBean and PersonalDetailsBean ... inside the PersonalDetailsBean is a List of EmailAddressBean's and a property of the EmailAddressBean is a public String emailAddress .... How on God's earth can I validate that that property is a valid email and required for every instance of EmailAddressBean contained in the List using validator.xml I have tried so many things Im starting to go mad see code below: createUser.jsp <html:form action="/createUserAccount" method="POST"> <table border="0" cellpadding="0" cellspacing="0" > <tr> <td valign="top" id=leftbanner > <table border="0"> <tr> <th colspan="3">Create a New User Account</th> </tr> <nested:nest property="userAccountDetails"> <tr> <td><bean:message key="label.createuser.username"/></td> <td><nested:text property="username" /></td> <td><nested:errors property="username"/></td> </tr> <tr> <td><bean:message key="label.createuser.password"/></td> <td><nested assword property="password" /></td> <td><nested:errors property="password"/></td> </tr> <tr> <td><bean:message key="label.createuser.confirmpassword"/></td> <td><nested assword property="confirmedPassword" /></td> <td><nested:errors property="confirmedPassword"/></td> </tr> </nested:nest> <nested:nest property="personalDetails"> <nested:iterate property="emailAddresses"> <tr> <td><bean:message key="label.createuser.email"/></td> <td><nested:text property="emailAddress"/></td> <td><nested:errors property="emailAddress"/></td> </tr> </nested:iterate> </nested:nest> <tr> <td><html:submit /></td> </tr> </table> </td> </tr> </table> </html:form> UserDetailsForm.java public class UserDetailsForm extends ValidatorForm { /* Standard serial version UID */ private static final long serialVersionUID = 1L; /* The data elements specific to the users account details */ private UserDetailsBean userAccountDetails; /* The data elements specific to the users personal details */ private PersonalDetailsBean personalDetails; /** * Constructor */ public UserDetailsForm() { userAccountDetails = new UserDetailsBean(); personalDetails = new PersonalDetailsBean(); } ... PersonalDetailsBean.java public class PersonalDetailsBean implements Serializable { /* Standard serial version UID */ private static final long serialVersionUID = -4583072476776502751L; /* Unique identifier for this personal details data set */ private String personalDetailsID; ... /* Users email addresses */ private List<EmailAddressBean> emailAddresses; public PersonalDetailsBean() { Factory factory = new Factory() { public Object create() { return new EmailAddressBean(); } }; emailAddresses = LazyList.decorate(new ArrayList<EmailAddressBean>(), factory); emailAddresses.add(new EmailAddressBean()); } ... EmailAddressBean.java public class EmailAddressBean implements Serializable { /* Standard serial version UID */ private static final long serialVersionUID = 6926416463088230275L; /* A unique id for this email address */ private String emailAddressId; /* A unique id which links this email address to the corresponding PersonalDetailsBean Object*/ private String personalDetailsId; /* The email address */ private String emailAddress; /* Is this emaiil the primary email address of the user to which it is associated */ private boolean primaryEmailAddress; ... struts-config.xml <form-beans> <form-bean name="userDetailsForm" type="struts.form.UserDetailsForm"> <form-property name="userAccountDetails" type="bean.profile.UserDetailsBean" /> <form-property name="personalDetails" type="bean.profile.PersonalDetailsBean" /> </form-bean> </form-beans> validator.xml <form name="userDetailsForm"> <field property="userAccountDetails.username" depends="required"> <arg key="key.error.username"/> </field> <field property="userAccountDetails.password" depends="required,mask"> <arg key="key.error.password"/> <var> <var-name>mask</var-name> <var-value>^[0-9a-zA-Z]*$</var-value> </var> </field> <field property="userAccountDetails.confirmedPassword" depends="required,mask"> <arg key="key.error.confirmpassword"/> <var> <var-name>mask</var-name> <var-value>^[0-9a-zA-Z]*$</var-value> </var> </field> <!-- What do I put here to validate the emailAddress Property for every Instance of the Bean in the List, indexes will not cut it because the List varies in length --> <field property="personalDetails.emailAddresses.emailAddress" depends="required, email"> <arg key="key.error.emailaddress"/> </field> </form> Please can someone tell me if this is even doable ... Ive started to think ive looked around for days for something that just cant be done If I have not been clear please let me know and I will try to explain in greater or less ambigious terms. Thanks, James
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Officially, Struts doesn't support validation for indexed properties. However, I have a feeling the following might work: I'm pretty sure this will at least validate the first element of the collection, but I have had others tell me that it will also validate the other elements as well. I don't have time right now to test it for myself, but if you're willing to be a "guinea pig", give it a try and tell us whether it works for all elements or not.
|
Merrill
Consultant, Sima Solutions
|
 |
Jimmy Chuck
Greenhorn
Joined: Feb 23, 2007
Posts: 2
|
|
|
Thank you so much! The emailAddresses[0] does not validate all elements in the collection, but at least you have told me its not supported! Im very grateful. thanks again
|
 |
 |
|
|
subject: Validate over a collection of beans inside a bean.
|
|
|