Example usage for org.apache.commons.validator ValidatorResult getActions

List of usage examples for org.apache.commons.validator ValidatorResult getActions

Introduction

In this page you can find the example usage for org.apache.commons.validator ValidatorResult getActions.

Prototype

public Iterator<String> getActions() 

Source Link

Document

Return an Iterator of the action names contained in this Result.

Usage

From source file:cn.com.ihuimobile.test.validate.ValidateExample.java

/**
 * Dumps out the Bean in question and the results of validating it.
 *//*from  w w  w.j  a  va  2 s  . c o m*/
@SuppressWarnings("unchecked")
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");
    }

}