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:
Jon Brisbin
2013-03-05 14:05:53 -06:00
parent 810b2144c8
commit 05f11a6cf9
8 changed files with 141 additions and 61 deletions

View File

@@ -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")

View File

@@ -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");
}
}

View File

@@ -0,0 +1 @@
not.blank = Field {1} cannot be blank