February 2005
Intermediate to advanced
528 pages
12h 53m
English
You need to add a custom ad hoc validation check to a Validator
ActionForm.
Extend the ValidatorForm or
ValidatorActionForm and override the
validate( ) method, ensuring you call
super.validate( ) to
perform the Validator's validation. (See Example 8-14.)
Example 8-14. Extending the ValidatorForm
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public final class MyForm extends ValidatorForm {
private String foo;
private String bar;
public String getFoo( ) {
return foo;
}
public void setFoo(String s) {
foo = s;
}
public String getBar( ) {
return bar;
}
public void setBar(String s) {
bar = s;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// Perform validator framework validations
ActionErrors errors = super.validate(mapping, request);
// Add crossfield and business validations here
if (!checkFooBarValid(foo, bar)) {
errors.add("foo", new ActionError("errors.invalidFooBar"));
}
// additional validations ...
return errors;
}
private boolean checkFooBarValid(Foo foo, Bar bar) {
boolean valid = false;
// perform custom validation
valid = FooBarUtil.checkFooBar(foo, bar);
return valid;
}
}If you're using a dynamic form defined in the
struts-config.xml, extend the type being used
(e.g., DynaValidatorForm ...