bean-validation

JSR-303 bean validation with Spring does not kick in

痴心易碎 提交于 2019-11-29 07:37:01
I've configured a JSR-303 custom validator following what's given in the docs ( http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/validation.html ), complete with LocalValidatorFactoryBean and Hibernate validator on the classpath. However, my validator just refuses to kick in. I've put up a dirt simple test project here ( https://github.com/abhijitsarkar/java/tree/master/spring-jsr-303 ), along with a failing unit test. Should you decide to take a look, just clone it and run gradlew clean test from the root directory. I'm using Spring framework 4.0.2.RELEASE and Hibernate

Using a custom ResourceBundle with Hibernate Validator

拜拜、爱过 提交于 2019-11-29 07:36:57
问题 I'm trying to set up a custom message source for Hibernate Validator 4.1 through Spring 3.0. I've set up the necessary configuration: <!-- JSR-303 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="validationMessageSource" ref="messageSource"/> </bean> The translations are served from my message source, but it seems that the replacement tokens in the messages themselves are looked up in the message source, i.e. for a: my

Spring Boot JSR303 message code in annotation getting ignored

混江龙づ霸主 提交于 2019-11-29 06:55:21
In my Spring Boot app I have a backing bean where I am using JSR303 validation. In the annotation, I have specified the message code: @NotBlank(message = "{firstname.isnull}") private String firstname; Then in my message.properties I have specified: firstname.isnull = Firstname cannot be empty or blank My JavaConfig for the messageSource is: @Bean(name = "messageSource") public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; }

how to create a ConstraintValidator for List

寵の児 提交于 2019-11-29 05:05:38
I have a simple validator to validate that a String value is part of a predefined list: public class CoBoundedStringConstraints implements ConstraintValidator<CoBoundedString, String> { private List<String> m_boundedTo; @Override public void initialize(CoBoundedString annotation) { m_boundedTo = FunctorUtils.transform(annotation.value(), new ToLowerCase()); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null ) { return true; } context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate("should be one of " + m

Validating double and float values using Hibernate Validator - bean validation

耗尽温柔 提交于 2019-11-29 02:53:14
问题 I'm looking for a way to validate a java.lang.Double field in the Spring command bean for its maximum and minimum values (a value must lie between a given range of values) like, public final class WeightBean { @Max(groups={ValidationGroup.class}, value=Double.MAX_VALUE, message="some key or default message") @Min(groups={ValidationGroup.class}, value=1D, message="some key or default message") private Double txtWeight; //Getter and setter. public interface ValidationGroup{} } But both @Max and

How to combine JSR-303 and Spring Validator class in a service layer?

≯℡__Kan透↙ 提交于 2019-11-29 02:31:45
I have some model class public class Account { @Email private String email; @NotNull private String rule; } and spring-validator public class AccountValidator implements Validator { @Override public boolean supports(Class aClass) { return Account.class.equals(aClass); } @Override public void validate(Object obj, Errors errors) { Account account = (Account) obj; ValidationUtils.rejectIfEmpty(errors, "email", "email.required"); ValidationUtils.rejectIfEmpty(errors, "rule", "rule.required"); complexValidateRule(account.getRule(), errors); } private void complexValidateRule(String rule, Errors

Hibernate @OneToOne @NotNull

与世无争的帅哥 提交于 2019-11-28 23:31:40
Is it valid to declare @OneToOne and @NotNull on both sides of a relationship, such as: class ChangeEntry { @OneToOne(cascade=CascadeType.ALL) @NotNull ChangeEntryDetails changeEntryDetails; public void addDetails(ChangeEntryDetails details) { this.changeEntryDetails = details; details.setChangeEntry(this); } } class ChangeEntryDetails { @OneToOne(cascase=CascadeType.ALL) @NotNull ChangeEntry changeEntry; public void setChangeEntry(ChangeEntry changeEntry) { this.changeEntry = changeEntry; } } I can't find anything that says this is invalid, but it seems that during persistence at least one

How to handle Resource validation in REST webservices?

蹲街弑〆低调 提交于 2019-11-28 21:15:22
问题 I'm building a REST webservice in Java using Spring, Jersey and Hibernate (JPA). Now I'm trying to add support for validation in my resources. JSR-303 (Bean validation) naturally appeared as a suitable choice (I'm using Hibernate Validator which is the reference implementation of JSR-303). However, I'm trying to consolidate some concepts first. It seems to me that there are at least two kinds of possible validations: Regular validation on fields , e.g. check that a property isn't null, check

javax.validation.ValidationException: Unable to find default provider

半腔热情 提交于 2019-11-28 19:10:26
I am currently working on Spring MVC web app and trying to hook up validation using the @Valid annotation. When I fire up the application I'm getting the following exception: javax.validation.ValidationException: Unable to find a default provider I have Hibernate Validator 3.1.0.GA on the classpath as well as javax validation 1.0.0.GA, Hibernate Core 3.3.1.GA and Hibernate Annotations 3.4.0.GA. Is there an incompatiblity in those versions that I'm not seeing, or can anyone think of any reason why I'm still getting this exception with Hibernate Validator on the class path? Cheers, Caps

JSF 2.0: How to skip JSR-303 bean validation?

為{幸葍}努か 提交于 2019-11-28 18:54:55
How to skip JSR-303 Bean validation with JSF, when a button is clicked? A bit lengthy question to explain a few approaches... Consider a list in a form: <h:form id="form"> <h:commandButton value="Add row"> <f:ajax execute="foo" listener="#{bean.add()}" render="foo" /> </h:commandButton> <h:dataTable id="foo" var="foo" value="#{bean.foos}"> <h:column> Name: <h:inputText id="field" value="#{foo.name}" required="true" /> <h:messages for="field" /> </h:column> <h:column> <h:commandButton value="Remove"> <f:ajax execute=":form:foo" listener="#{bean.remove(foo)}" render=":form:foo" /> </h