Tuesday, October 27, 2009

Recipe 8.10. Adding a Custom Validation to a Validator Form










Recipe 8.10. Adding a Custom Validation to a Validator Form




Problem



You need to add a custom ad hoc validation check to a Validator
ActionForm.





Solution



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), and override the
validate( ) method as the class does in Example 8-15.




Example 8-15. Extending the DynaValidatorForm

package com.mycompany.myapp;

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.DynaValidatorForm;

public final class MyDynaForm extends DynaValidatorForm {
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// Perform validator framework validations
ActionErrors errors = super.validate(mapping, request);

// get the needed property values
String foo = (String) get("foo");
String bar = (String) get("bar");

// 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;
}
}





Then change the form-bean element to use this new
class:



<form-bean name="SomeForm" 
type="com.mycompany.myapp.MyDynaForm">
<form-property name="foo" type="java.lang.String"/>
<form-property name="bar" type="java.lang.String"/>
</form-bean>






Discussion



Though the Validator offloads much of your hand-written code to a
configuration file, more complex validations may be required.
Two general types of validation exist:
syntactic and
semantic. Syntactic validation verifies
the syntax of the data, and semantic validation verifies that the
data is meaningful from a business sense. The Validator does
syntactic checks well but isn't purposed for
semantic validation. Usually, this validation needs to be
hand-written.



Fortunately, the Validator doesn't prohibit you from
providing additional custom validations. For conventional hand-coded
ActionForms that extend
ValidatorForm or
ValidatorActionForm, override the
validate( ) method to add the additional
validation checks. In your validate( ) method,
call super.validate( ) to allow the Validator to
perform its verifications. Then perform additional validations in
your own method as required. If an error occurs, add it to the
ActionErrors object returned from
super.validate( ).



If you are using the Validator with dynamic action forms, then your
form bean typically uses a type of
DynaValidatorForm or
DynaValidatorActionForm. Apply the same technique
for extending these classes as with conventional nondynamic form
classes. You extend the form class and override the
validate method. To access properties of a
DynaValidatorForm, you'll need to
use the
get(property)
methods.





See Also



Recipe 5.1 discusses how to create and use
dynamic action forms.












    No comments:

    Post a Comment