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

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

Introduction

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

Prototype

public void setOnlyReturnErrors(boolean onlyReturnErrors) 

Source Link

Document

Configures which Fields the Validator returns from the validate() method.

Usage

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

/**
 * This is the main method that will be called to initialize the Validator, create some sample beans, and
 * run the Validator against them.//from w  w w  .  j ava 2  s  .c o m
 */
@Test
public void mainTest() throws ValidatorException, IOException, SAXException {

    InputStream in = null;
    ValidatorResources resources = null;

    try {

        // Create a new instance of a ValidatorResource, then get a stream
        // handle on the XML file with the actions in it, and initialize the
        // resources from it.  This would normally be done by a servlet
        // run during JSP initialization or some other application-startup
        // routine.
        in = ValidateExample.class.getResourceAsStream("validator-example.xml");
        resources = new ValidatorResources(in);

    } finally {
        // Make sure we close the input stream.
        if (in != null) {
            in.close();
        }
    }

    // Create a test bean to validate against.
    ValidateBean bean = new ValidateBean();

    // Create a validator with the ValidateBean actions for the bean
    // we're interested in.
    Validator validator = new Validator(resources, "ValidateBean");

    // Tell the validator which bean to validate against.
    validator.setParameter(Validator.BEAN_PARAM, bean);

    ValidatorResults results = null;

    // Run the validation actions against the bean.  Since all of the properties
    // are null, we expect them all to error out except for street2, which has
    // no validations (it's an optional property)

    results = validator.validate();
    System.out.println(results);
    printResults(bean, results, resources);

    // Now set all the required properties, but make the age a non-integer.
    // You'll notice that age will pass the required test, but fail the int
    // test.
    bean.setLastName("Tester");
    bean.setFirstName("John");
    bean.setStreet1("1 Test Street");
    bean.setCity("Testville");
    bean.setState("TE");
    bean.setPostalCode("12345");
    bean.setAge("Too Old");
    results = validator.validate();
    printResults(bean, results, resources);

    // Now only report failed fields
    validator.setOnlyReturnErrors(true);
    results = validator.validate();
    printResults(bean, results, resources);

    // Now everything should pass.
    validator.setOnlyReturnErrors(false);
    bean.setAge("123");
    results = validator.validate();
    printResults(bean, results, resources);
}

From source file:org.apache.commons.validator.example.ValidateExample.java

/**
 * This is the main method that will be called to initialize the Validator, create some sample beans, and
 * run the Validator against them.//from  w  ww  .  j a va  2s. c  om
 */
public static void main(String[] args) throws ValidatorException, IOException, SAXException {

    InputStream in = null;
    ValidatorResources resources = null;

    try {

        // Create a new instance of a ValidatorResource, then get a stream
        // handle on the XML file with the actions in it, and initialize the
        // resources from it.  This would normally be done by a servlet
        // run during JSP initialization or some other application-startup
        // routine.
        in = ValidateExample.class.getResourceAsStream("validator-example.xml");
        resources = new ValidatorResources(in);

    } finally {
        // Make sure we close the input stream.
        if (in != null) {
            in.close();
        }
    }

    // Create a test bean to validate against.
    //ValidateBean bean = new ValidateBean();
    LazyDynaClass dynaClass = new LazyDynaClass();
    dynaClass.add("lastName", String.class);
    dynaClass.add("firstName", String.class);
    dynaClass.add("street1", String.class);
    dynaClass.add("city", String.class);
    dynaClass.add("state", String.class);
    dynaClass.add("postalCode", String.class);
    dynaClass.add("age", String.class);
    LazyDynaBean dynaBean = new LazyDynaBean(dynaClass);

    // Create a validator with the ValidateBean actions for the bean
    // we're interested in.
    Validator validator = new Validator(resources, "ValidateBean");

    // Tell the validator which bean to validate against.
    validator.setParameter(Validator.BEAN_PARAM, dynaBean);

    ValidatorResults results = null;

    // Run the validation actions against the bean.  Since all of the properties
    // are null, we expect them all to error out except for street2, which has
    // no validations (it's an optional property)

    results = validator.validate();
    printResults(dynaBean, results, resources);

    // Now set all the required properties, but make the age a non-integer.
    // You'll notice that age will pass the required test, but fail the int
    // test.
    dynaBean.set("lastName", "Tester");
    dynaBean.set("firstName", "John");
    dynaBean.set("street1", "1 Test Street");
    dynaBean.set("city", "Testville");
    dynaBean.set("state", "TE");
    dynaBean.set("postalCode", "12345");
    dynaBean.set("age", "Too Old");
    results = validator.validate();
    printResults(dynaBean, results, resources);

    // Now only report failed fields
    validator.setOnlyReturnErrors(true);
    results = validator.validate();
    printResults(dynaBean, results, resources);

    // Now everything should pass.
    validator.setOnlyReturnErrors(false);
    dynaBean.set("age", "123");
    results = validator.validate();
    printResults(dynaBean, results, resources);
}