Search in sources :

Example 1 with EscapedErrors

use of org.springframework.web.bind.EscapedErrors in project spring-framework by spring-projects.

the class RequestContext method getErrors.

/**
	 * Retrieve the Errors instance for the given bind object.
	 * @param name name of the bind object
	 * @param htmlEscape create an Errors instance with automatic HTML escaping?
	 * @return the Errors instance, or {@code null} if not found
	 */
public Optional<Errors> getErrors(String name, boolean htmlEscape) {
    if (this.errorsMap == null) {
        this.errorsMap = new HashMap<>();
    }
    // Since there is no Optional orElse + flatMap...
    Optional<Errors> optional = Optional.ofNullable(this.errorsMap.get(name));
    optional = optional.isPresent() ? optional : getModelObject(BindingResult.MODEL_KEY_PREFIX + name);
    return optional.map(errors -> {
        if (errors instanceof BindException) {
            return ((BindException) errors).getBindingResult();
        } else {
            return errors;
        }
    }).map(errors -> {
        if (htmlEscape && !(errors instanceof EscapedErrors)) {
            errors = new EscapedErrors(errors);
        } else if (!htmlEscape && errors instanceof EscapedErrors) {
            errors = ((EscapedErrors) errors).getSource();
        }
        this.errorsMap.put(name, errors);
        return errors;
    });
}
Also used : Errors(org.springframework.validation.Errors) UriTemplate(org.springframework.web.util.UriTemplate) NoSuchMessageException(org.springframework.context.NoSuchMessageException) TimeZone(java.util.TimeZone) HashMap(java.util.HashMap) BindingResult(org.springframework.validation.BindingResult) HtmlUtils(org.springframework.web.util.HtmlUtils) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) Locale(java.util.Locale) Map(java.util.Map) EscapedErrors(org.springframework.web.bind.EscapedErrors) Optional(java.util.Optional) MessageSourceResolvable(org.springframework.context.MessageSourceResolvable) BindException(org.springframework.validation.BindException) MessageSource(org.springframework.context.MessageSource) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) Errors(org.springframework.validation.Errors) EscapedErrors(org.springframework.web.bind.EscapedErrors) BindException(org.springframework.validation.BindException) EscapedErrors(org.springframework.web.bind.EscapedErrors)

Example 2 with EscapedErrors

use of org.springframework.web.bind.EscapedErrors in project spring-framework by spring-projects.

the class RequestContext method getErrors.

/**
	 * Retrieve the Errors instance for the given bind object.
	 * @param name name of the bind object
	 * @param htmlEscape create an Errors instance with automatic HTML escaping?
	 * @return the Errors instance, or {@code null} if not found
	 */
public Errors getErrors(String name, boolean htmlEscape) {
    if (this.errorsMap == null) {
        this.errorsMap = new HashMap<>();
    }
    Errors errors = this.errorsMap.get(name);
    boolean put = false;
    if (errors == null) {
        errors = (Errors) getModelObject(BindingResult.MODEL_KEY_PREFIX + name);
        // Check old BindException prefix for backwards compatibility.
        if (errors instanceof BindException) {
            errors = ((BindException) errors).getBindingResult();
        }
        if (errors == null) {
            return null;
        }
        put = true;
    }
    if (htmlEscape && !(errors instanceof EscapedErrors)) {
        errors = new EscapedErrors(errors);
        put = true;
    } else if (!htmlEscape && errors instanceof EscapedErrors) {
        errors = ((EscapedErrors) errors).getSource();
        put = true;
    }
    if (put) {
        this.errorsMap.put(name, errors);
    }
    return errors;
}
Also used : Errors(org.springframework.validation.Errors) EscapedErrors(org.springframework.web.bind.EscapedErrors) BindException(org.springframework.validation.BindException) EscapedErrors(org.springframework.web.bind.EscapedErrors)

Aggregations

BindException (org.springframework.validation.BindException)2 Errors (org.springframework.validation.Errors)2 EscapedErrors (org.springframework.web.bind.EscapedErrors)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 TimeZone (java.util.TimeZone)1 MessageSource (org.springframework.context.MessageSource)1 MessageSourceResolvable (org.springframework.context.MessageSourceResolvable)1 NoSuchMessageException (org.springframework.context.NoSuchMessageException)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 Assert (org.springframework.util.Assert)1 BindingResult (org.springframework.validation.BindingResult)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 HtmlUtils (org.springframework.web.util.HtmlUtils)1 UriTemplate (org.springframework.web.util.UriTemplate)1