You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by hem hem <he...@yahoo.com> on 2005/11/07 22:48:08 UTC
how to use Struts validator to this action form-please help me
hi all,
i am new to struts validation.
i foung some nice tutorials which validates action forms using validator-rules.xml and validation.xml(which connects validator-rules.xml and formbean name in stuts-config.xml).
but my form bean looks like this(i am using 2 more beans in my action form) which results in nested properties.
here is my UsersForm:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;
public class UsersForm extends ValidatorForm {
private static final int MAX_NUMBER_OF_USERS = 5;
private int numberOfUsers = 0;
private static final String mandatoryFields = "madatory field"; // Mandatory error category
private static final String dateFormat = "date error"; // user error category
private ProductBean product = new ProductBean();
private String purpose = "";
private List users = new ArrayList(MAX_NUMBER_OF_USERS);
public UsersForm() {
super();
for (int i = 0; i < MAX_NUMBER_OF_USERS; i++) {
UsersBean ub = new UsersBean();
users.add(ub);
}
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = null;
boolean mandatoryError = false;
Iterator i = users.iterator();
while (i.hasNext()) {
UsersBean bean = (UsersBean)i.next();
if (bean.getSurname() != null && !bean.getSurname().equals("")) {
++numberOfUsers;
if (! mandatoryError){
if ((bean.getDob() == null) || (bean.getDob().equals(""))) {
errors = addError(errors, UsersForm.mandatoryFields);
mandatoryError = true;
} else {
SimpleDateFormat sdf = new SimpleDateFormat(DateHelper.dateFormatddmmyyyy);
sdf.setLenient(false);
try {
Date dob = sdf.parse(bean.getDob());
} catch (ParseException e) {
errors = addError(errors, UsersForm.dateFormat);
}
}
}
}
if (numberOfUsers == 0 && mandatoryError == false) {
errors = addError(errors, UsersForm.mandatoryFields);
mandatoryError = true;
}
}
return errors;
}
private ActionErrors addError(ActionErrors errors, String messageKey) {
if (errors == null) {
errors = new ActionErrors();
}
ActionMessage error = new ActionMessage(messageKey);
errors.add(messageKey, error);
return errors;
}
/**
* @return
*/
public String getPurpose() {
return basis;
}
/**
* @param string
*/
public void setPurpose(String string) {
purpose = string;
}
/**
* @return
*/
public ProductBean getProduct() {
return product;
}
/**
* @param bean
*/
public void setProduct(ProductBean bean) {
product = bean;
}
/**
* @return
*/
public List getUsers() {
return users;
}
/**
* @param list
*/
public void setUsers(List list) {
users = list;
}
public void setUsers(int index, Object value) {
users.add(index, value);
}
public Object getUsers(int index) {
return users.get(index);
}
/**
* @return
*/
public int getNumberOfUsers() {
return numberOfUsers;
}
}
and here is my ProductBean:
public class ProductBean {
private String code;
/**
*
*/
public ProductBean() {
super();
}
public ProductBean(String aCode) {
this.code = aCode;
}
/**
* @return
*/
public String getCode() {
return code;
}
/**
* @param string
*/
public void setCode(String string) {
code = string;
}
}
and here is my UsersBean:
public class UsersBean {
private String title = null;
private String forename = null;
private String surname = null;
private String dob = null;
public UsersBean() {
super();
}
/**
* @return
*/
public String getDob() {
return dob;
}
/**
* @return
*/
public String getTitle() {
return title;
}
/**
* @param string
*/
public void setDob(String string) {
dob = string;
}
/**
* @param string
*/
public void setTitle(String string) {
title = string;
}
/**
* @return
*/
public String getForename() {
return forename;
}
/**
* @return
*/
public String getSurname() {
return surname;
}
/**
* @param string
*/
public void setForename(String string) {
forename = string;
}
/**
* @param string
*/
public void setSurname(String string) {
surname = string;
}
}
i want to validate all the fields 5users*4 =20 and purpose filed and code in productbean and save the errors?
can any one give me an out line idea to do validation using struts(using the files validator-rules.xml,validation.xml and struts-config.xml where we write form bean definitions
regards and many thanks advance
---------------------------------
Yahoo! FareChase - Search multiple travel sites in one click.