Fix for #71. Made validation messages more robust and synced them between the Spring Validator based validation and the JSR-303 validation annotations. The output differed before but is now consistent.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package org.springframework.data.rest.example;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||
import org.springframework.data.rest.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.example.jpa.Person;
|
||||
import org.springframework.data.rest.example.jpa.PersonValidator;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Resource;
|
||||
@@ -17,6 +20,17 @@ import org.springframework.hateoas.ResourceProcessor;
|
||||
@ImportResource("classpath:META-INF/spring/security-config.xml")
|
||||
public class RestExporterExampleRestConfig extends RepositoryRestMvcConfiguration {
|
||||
|
||||
@Bean public MessageSource messageSource() {
|
||||
ReloadableResourceBundleMessageSource msgsrc = new ReloadableResourceBundleMessageSource();
|
||||
msgsrc.setBasename("/WEB-INF/classes/ValidationMessages");
|
||||
msgsrc.setFallbackToSystemLocale(false);
|
||||
return msgsrc;
|
||||
}
|
||||
|
||||
@Bean public PersonValidator beforeCreatePersonValidator() {
|
||||
return new PersonValidator();
|
||||
}
|
||||
|
||||
@Override protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
|
||||
config.addResourceMappingForDomainType(Person.class)
|
||||
.addResourceMappingFor("lastName")
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.springframework.data.rest.example.jpa;
|
||||
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class PersonValidator implements Validator {
|
||||
|
||||
@Override public boolean supports(Class<?> clazz) {
|
||||
return Person.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
@Override public void validate(Object target, Errors errors) {
|
||||
ValidationUtils.rejectIfEmpty(errors, "firstName", "not.blank");
|
||||
ValidationUtils.rejectIfEmpty(errors, "lastName", "not.blank");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
not.blank = Field {1} cannot be blank
|
||||
Reference in New Issue
Block a user