Use code in error query parameter. Closes #5
End users should have the possibility to provide their own error messages. Therefore not the message, instead the error code is provided.pull/21/head
parent
d6ba02e29e
commit
6755dc007c
|
@ -5,7 +5,10 @@ import org.springframework.beans.factory.annotation.Value;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
|
@ -31,4 +34,20 @@ public class MailRelayApplication {
|
|||
return registrationBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageSource messageSource() {
|
||||
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||
|
||||
messageSource.setBasename("classpath:messages/messages");
|
||||
messageSource.setDefaultEncoding("UTF-8");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalValidatorFactoryBean getValidator() {
|
||||
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
|
||||
bean.setValidationMessageSource(messageSource());
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,17 +16,17 @@ import java.time.LocalDateTime;
|
|||
@NoArgsConstructor
|
||||
public class MessageForm {
|
||||
|
||||
@NotBlank
|
||||
@NotBlank(message = "{field.notblank}")
|
||||
private String token;
|
||||
|
||||
@NotBlank
|
||||
@NotBlank(message = "{field.notblank}")
|
||||
private String subject;
|
||||
|
||||
@NotBlank
|
||||
@NotBlank(message = "{field.notblank}")
|
||||
private String content;
|
||||
|
||||
@Pattern(regexp = StringUtils.EMAIL_RFC_5322_REGEX)
|
||||
@NotBlank
|
||||
@Pattern(regexp = StringUtils.EMAIL_RFC_5322_REGEX, message = "{field.pattern.email}")
|
||||
@NotBlank(message = "{field.notblank.email}")
|
||||
private String from;
|
||||
|
||||
|
||||
|
|
|
@ -54,14 +54,15 @@ public class MessageFormController {
|
|||
String queryParams = bindingResult.getAllErrors().stream()
|
||||
.map(ValidationError::fromObjectError)
|
||||
.map(MessageFormController::mapValidationErrorToQueryParam)
|
||||
.distinct()
|
||||
.collect(Collectors.joining("&"));
|
||||
String origin = form.getOnError() != null ? form.getOnError() : referer;
|
||||
|
||||
return origin + "?" + URLEncoder.encode(queryParams, StandardCharsets.UTF_8);
|
||||
return origin + "?" + queryParams;
|
||||
}
|
||||
|
||||
private static String mapValidationErrorToQueryParam(ValidationError error) {
|
||||
return String.format("%s=%s", error.getTarget(), error.getMessage());
|
||||
return String.format("%s=%s", error.getTarget(), URLEncoder.encode(error.getCode(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -14,12 +14,15 @@ public class ValidationError {
|
|||
private String target;
|
||||
private String message;
|
||||
private Object rejected;
|
||||
private String code;
|
||||
|
||||
public static ValidationError fromObjectError(ObjectError error) {
|
||||
FieldError fieldError = (FieldError) error;
|
||||
return new ValidationError(
|
||||
((FieldError)error).getField(),
|
||||
fieldError.getField(),
|
||||
error.getDefaultMessage(),
|
||||
((FieldError) error).getRejectedValue()
|
||||
fieldError.getRejectedValue(),
|
||||
fieldError.getCode()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
field.notblank=This field is mandatory
|
||||
field.notblank.email=Please provide a valid email
|
||||
field.pattern=Invalid format
|
||||
field.pattern.email=Please provide a valid email
|
|
@ -0,0 +1,4 @@
|
|||
field.notblank=Dies ist ein Pflichtfeld
|
||||
field.notblank.email=Bitte geben Sie eine gültige Email ein
|
||||
field.pattern=Ungültiges Format
|
||||
field.pattern.email=Bitte geben Sie eine gültige Email ein
|
Loading…
Reference in New Issue