Example usage for org.apache.commons.validator Validator setFieldName

List of usage examples for org.apache.commons.validator Validator setFieldName

Introduction

In this page you can find the example usage for org.apache.commons.validator Validator setFieldName.

Prototype

public void setFieldName(String fieldName) 

Source Link

Document

Sets the name of the field to validate in a form (optional)

Usage

From source file:org.openmobster.core.common.validation.ObjectValidator.java

/**
 * Validates the value of the specified Field on the target Object
 * //from   w  ww. j a  va2  s .c  o  m
 * @param object
 * @param fieldName
 * @return a Set of Validation Error Keys
 * @throws ValidationException
 */
public Set<String> validate(Object object, String fieldName) throws ValidationException {
    try {
        Set<String> errorKeys = new HashSet<String>();
        String objectId = object.getClass().getName();

        //Setup the Validator
        Validator validator = new Validator(this.validatorResources, objectId);
        validator.setParameter(Validator.BEAN_PARAM, object);
        validator.setFieldName(fieldName);

        ValidatorResults results = validator.validate();

        Form form = this.validatorResources.getForm(Locale.getDefault(), objectId);
        Iterator propertyNames = results.getPropertyNames().iterator();
        while (propertyNames.hasNext()) {
            String property = (String) propertyNames.next();
            ValidatorResult result = results.getValidatorResult(property);
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            while (keys.hasNext()) {
                String actionName = (String) keys.next();
                if (!result.isValid(actionName)) {
                    Field field = form.getField(property);
                    Arg[] args = field.getArgs(actionName);
                    if (args != null) {
                        for (int i = 0; i < args.length; i++) {
                            errorKeys.add(args[i].getKey());
                        }
                    }
                }
            }
        }

        return errorKeys;
    } catch (Exception e) {
        log.error(this, e);
        throw new ValidationException(e);
    }
}