in src/example/org/apache/commons/validator/example/ValidateExample.java [131:193]
public static void printResults(
ValidateBean bean,
ValidatorResults results,
ValidatorResources resources) {
boolean success = true;
// Start by getting the form for the current locale and Bean.
Form form = resources.getForm(Locale.getDefault(), "ValidateBean");
System.out.println("\n\nValidating:");
System.out.println(bean);
// Iterate over each of the properties of the Bean which had messages.
Iterator<String> propertyNames = results.getPropertyNames().iterator();
while (propertyNames.hasNext()) {
String propertyName = propertyNames.next();
// Get the Field associated with that property in the Form
Field field = form.getField(propertyName);
// Look up the formatted name of the field from the Field arg0
String prettyFieldName = apps.getString(field.getArg(0).getKey());
// Get the result of validating the property.
ValidatorResult result = results.getValidatorResult(propertyName);
// Get all the actions run against the property, and iterate over their names.
Iterator<String> keys = result.getActions();
while (keys.hasNext()) {
String actName = keys.next();
// Get the Action for that name.
ValidatorAction action = resources.getValidatorAction(actName);
// If the result is valid, print PASSED, otherwise print FAILED
System.out.println(
propertyName
+ "["
+ actName
+ "] ("
+ (result.isValid(actName) ? "PASSED" : "FAILED")
+ ")");
//If the result failed, format the Action's message against the formatted field name
if (!result.isValid(actName)) {
success = false;
String message = apps.getString(action.getMsg());
Object[] args = { prettyFieldName };
System.out.println(
" Error message will be: "
+ MessageFormat.format(message, args));
}
}
}
if (success) {
System.out.println("FORM VALIDATION PASSED");
} else {
System.out.println("FORM VALIDATION FAILED");
}
}