
com.mitchellbosecke.pebble.spring.extension.function.bindingresult.GetAllErrorsFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pebble-spring3 Show documentation
Show all versions of pebble-spring3 Show documentation
Pebble Integration with Spring 3
The newest version!
/*******************************************************************************
* Copyright (c) 2013 by Mitchell Bösecke
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
package com.mitchellbosecke.pebble.spring.extension.function.bindingresult;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
*
* Function available to templates in Spring MVC applications in order to access
* the BindingResult of a form
*
*
* @author Eric Bussieres
*/
public class GetAllErrorsFunction extends BaseBindingResultFunction {
public static final String FUNCTION_NAME = "getAllErrors";
private final MessageSource messageSource;
public GetAllErrorsFunction(MessageSource messageSource) {
super(PARAM_FORM_NAME);
this.messageSource = messageSource;
}
@Override
public Object execute(Map args) {
String formName = (String) args.get(PARAM_FORM_NAME);
EvaluationContext context = (EvaluationContext) args.get("_context");
Locale locale = context.getLocale();
BindingResult bindingResult = this.getBindingResult(formName, context);
return this.constructErrorMessage(locale, bindingResult);
}
private List constructErrorMessage(Locale locale, BindingResult bindingResult) {
List errorMessages = new ArrayList<>();
if (bindingResult != null) {
for (ObjectError error : bindingResult.getAllErrors()) {
String msg = this.messageSource.getMessage(error.getCode(), error.getArguments(),
error.getDefaultMessage(), locale);
errorMessages.add(msg);
}
}
return errorMessages;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy