Example usage for org.springframework.web.bind WebDataBinder validate

List of usage examples for org.springframework.web.bind WebDataBinder validate

Introduction

In this page you can find the example usage for org.springframework.web.bind WebDataBinder validate.

Prototype

public void validate(Object... validationHints) 

Source Link

Document

Invoke the specified Validators, if any, with the given validation hints.

Usage

From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java

/**
 * Validate the model attribute if applicable.
 * <p>/*from ww  w.j a v  a 2 s.  c  o  m*/
 * The default implementation checks for {@code @javax.validation.Valid}.
 * 
 * @param binder the DataBinder to be used
 * @param parameter the method parameter
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation annot : annotations) {
        if (annot.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = AnnotationUtils.getValue(annot);
            binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
        }
    }
}

From source file:org.focusns.common.web.widget.mvc.method.WidgetModelAttributeMethodProcessor.java

@Override
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    ////from  ww w .  j  a v a2  s  . c om
    WebRequest webRequest = webRequestLocal.get();
    String groupsStr = webRequest.getParameter("groups");
    if (StringUtils.hasText(groupsStr)) {
        List<Object> hintList = new ArrayList<Object>();
        String[] groups = StringUtils.commaDelimitedListToStringArray(groupsStr);
        for (String group : groups) {
            try {
                hintList.add(ClassUtils.forName(group, getClass().getClassLoader()));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //
        hintList.add(Default.class);
        //
        Annotation[] annotations = parameter.getParameterAnnotations();
        for (Annotation annot : annotations) {
            if (annot.annotationType().getSimpleName().startsWith("Valid")) {
                Object hints = hintList.toArray(new Object[hintList.size()]);
                binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            }
        }
    }
}

From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java

protected void validateComponent(WebDataBinder binder, MethodParameter parameter) throws BindException {

    boolean validateParameter = validateParameter(parameter);
    Annotation[] annotations = binder.getTarget().getClass().getAnnotations();
    for (Annotation annot : annotations) {
        if (annot.annotationType().getSimpleName().startsWith("Valid") && validateParameter) {
            Object hints = AnnotationUtils.getValue(annot);
            binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
        }/*w ww.  java2  s.  co  m*/
    }

    if (binder.getBindingResult().hasErrors()) {
        if (isBindExceptionRequired(binder, parameter)) {
            throw new BindException(binder.getBindingResult());
        }
    }
}

From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java

private void doBind(WebDataBinder binder, NativeWebRequest webRequest, boolean validate,
        Object[] validationHints, boolean failOnErrors) throws Exception {

    doBind(binder, webRequest);//from   w ww  . j av a2 s.com
    if (validate) {
        binder.validate(validationHints);
    }
    if (failOnErrors && binder.getBindingResult().hasErrors()) {
        throw new BindException(binder.getBindingResult());
    }
}

From source file:org.springframework.web.method.annotation.ModelAttributeMethodProcessor.java

/**
 * Validate the model attribute if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param binder the DataBinder to be used
 * @param parameter the method parameter declaration
 *//*from  w  w w . j a  v  a 2  s . c om*/
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation ann : annotations) {
        Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
            Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            binder.validate(validationHints);
            break;
        }
    }
}