Example usage for org.springframework.validation Validator validate

List of usage examples for org.springframework.validation Validator validate

Introduction

In this page you can find the example usage for org.springframework.validation Validator validate.

Prototype

void validate(Object target, Errors errors);

Source Link

Document

Validate the supplied target object, which must be of a Class for which the #supports(Class) method typically has (or would) return true .

Usage

From source file:com.springsource.hq.plugin.tcserver.serverconfig.ValidationUtils.java

/**
 * Performs validation of the given <code>Collection</code> of <code>Validator</code> instances. Each
 * <code>Validator</code> is treated as a self-validating domain object, i.e. the following is called upon each
 * <code>Validator</code>: <code>validator.validate(validator, errors)</code>. The given <code>identifier</code> is
 * used when pushing the path on the given <code>errors</code>, e.g. an identifier of 'foo' will result in
 * <code>foo[i]</code> being pushed, where i is the index of the validator in the given collection. The given
 * <code>errors</code> will be used to record any errors that are found.
 * /*  w  w w . j  ava  2 s .  co m*/
 * @see Validator#validate(Object, Errors)
 * 
 * @param selfValidatingItems The self-validating items to validate
 * @param identifier The identifier
 * @param errors Passed to each validator and to be used to record any errors
 */
public static void validateCollection(Collection<? extends Validator> selfValidatingItems, String identifier,
        Errors errors) {
    Iterator<? extends Validator> iterator = selfValidatingItems.iterator();
    int index = 0;
    while (iterator.hasNext()) {
        errors.pushNestedPath(String.format("%s[%d]", identifier, index++));
        Validator selfValidating = iterator.next();
        selfValidating.validate(selfValidating, errors);
        errors.popNestedPath();
    }
}

From source file:org.openmrs.module.kenyaemr.validator.EmailAddressValidatorTest.java

/**
 * Validates the input string//from   www. j a va2  s . c o  m
 * @param input the input
 * @return whether input is valid
 */
private static boolean invokeValidator(String input) {
    Validator validator = new EmailAddressValidator();
    Errors errors = new BeanPropertyBindingResult("test", "test");
    validator.validate(input, errors);
    return !errors.hasErrors();
}

From source file:org.openmrs.module.kenyaemr.validator.TelephoneNumberValidatorTest.java

/**
 * Validates the input string//from   w ww  . jav  a2  s  .  com
 * @param input the input
 * @return whether input is valid
 */
private static boolean invokeValidator(String input) {
    Validator validator = new TelephoneNumberValidator();
    Errors errors = new BeanPropertyBindingResult("test", "test");
    validator.validate(input, errors);
    return !errors.hasErrors();
}

From source file:com.acc.validator.CompositeValidator.java

@Override
public void validate(final Object object, final Errors errors) {
    for (final Validator v : validators) {
        if (v.supports(object.getClass())) {
            v.validate(object, errors);
        }//from ww  w. j  ava  2s.  com
    }
}

From source file:biz.deinum.multitenant.validation.CompositeValidator.java

/**
 * Validate the specified object using the validator registered for the object's class.
 */// www. ja  va  2s.  c om
public void validate(final Object target, final Errors errors) {
    for (final Validator v : this.validators) {
        if (v.supports(target.getClass())) {
            v.validate(target, errors);
        }
    }
}

From source file:com.healthcit.cacure.web.controller.LoginController.java

@RequestMapping(method = RequestMethod.POST)
public String onSubmit(UserCredentials userCredentials, BindingResult result, HttpServletRequest req,
        HttpServletResponse resp) {//  w  ww.j  a va 2  s. co  m

    Validator validator = new UserCredentialsValidator();
    validator.validate(userCredentials, result);

    if (result.hasErrors()) {
        req.setAttribute(ATTR_VALIDATION_ERR, Boolean.TRUE);
        return "login";
    }

    StringBuilder jSecurityRedirect = new StringBuilder("j_security_check");
    jSecurityRedirect.append("?userName=").append(userCredentials.getUserName());
    jSecurityRedirect.append("&password=").append(userCredentials.getPassword());
    try {
        resp.sendRedirect(jSecurityRedirect.toString());
    } catch (IOException e) {
        log.error("could not redirect to j_security_check");
    }

    return "login";
}

From source file:org.springmodules.validation.validator.CompoundValidator.java

/**
 * Validates the given object. All internal validators that support the given object class
 * will perform the actual validation.//from ww  w .ja  v a 2s.  co m
 *
 * @param obj The validated object.
 * @param errors A registry where validation errors are registered.
 * @see Validator#validate(Object, org.springframework.validation.Errors)
 */
public void validate(Object obj, Errors errors) {
    for (Iterator i = validators.iterator(); i.hasNext();) {
        Validator validator = (Validator) i.next();
        if (validator.supports(obj.getClass())) {
            validator.validate(obj, errors);
        }
    }
}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithApplicationContext() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "org/springmodules/validation/bean/beanValidator-tests.xml");

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    Validator validator = (Validator) context.getBean("validator");
    validator.validate(person, errors);

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());

}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithNonNullNullableValue() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "org/springmodules/validation/bean/beanValidator-tests.xml");

    Person person = new Person("Uri");
    person.setPhone("1234"); // should be validation error - length < 7
    BindException errors = new BindException(person, "person");

    Validator validator = (Validator) context.getBean("validator");
    validator.validate(person, errors);

    assertTrue(errors.hasFieldErrors("phone"));
    assertEquals(1, errors.getFieldErrorCount("phone"));
    assertEquals("Person.phone[min.length]", errors.getFieldError("phone").getCode());
}

From source file:org.easyj.rest.controller.AbstractGenericEntityController.java

protected ModelAndView save(E entity, BindingResult result, String viewName) {
    if (entity == null || result == null) {
        logger.debug("ERROR: Cannot save: some parameter is null entity[{}], result[{}]",
                new Object[] { entity, result });
        throw new BadRequestException(configMAV(entity, result, getEditViewName()));
    } else {// w w w.  jav  a  2  s .  c om
        for (Validator val : getValidators()) {
            val.validate(entity, result);
        }

        if (result.hasErrors()) {
            logger.debug("ERROR: Cannot save: missing or wrong parameters: ERRORS FOUND[{}]",
                    result.getErrorCount());
            throw new BadRequestException(configMAV(entity, result, getEditViewName()));
        } else {
            logger.debug("Entity SAVING: entity[" + entity + "]");
            E retEntity = persist(entity);
            logger.debug("Entity SAVED: entity[" + entity + "]");

            return configMAV(retEntity, result, viewName);
        }
    }

}