bean-validation

Hibernate Validator - @Length - How to specify separate message for min and max?

徘徊边缘 提交于 2019-12-02 20:37:42
I am using Hibernate validator for form validation in my web-app. I am using the @Length annotation for my String attribute as follows: @Length(min = 5, message = "The field must be at least 5 characters") private String myString; However, I have a need to display a different message if the String exceeds 50 characters. Is there a way to use the out-of-the-box @Length validator to do this? An example of what I would like to do (compiler will not let me) is as follows: @Length(min = 5, message = "The field must be at least 5 characters") @Length(max = 50, message = "The field must be less than

Conditional Bean Validation

橙三吉。 提交于 2019-12-02 19:51:03
问题 I am using Bean Validation in my JSF project. Now I have come across a situation in which I would like to validate a method ONLY when a preceding method is validated. I will give an example: @AssertTrue(message="{invalidCode}") private boolean isValidActivationCode() { ... } if(isValidActivationCode()) { @AssertTrue(message="{alreadyActivated}") private boolean isAlreadyActivated() { ... } } Since I will receive the Activation Code per parameter, I would like to validate it first. If it is

Not getting JSR303 annotations to work with Tomcat 7

断了今生、忘了曾经 提交于 2019-12-02 19:32:55
问题 After a couple hours of Google and a couple of tutorials I'm beat... It's not the fact that I'm getting errors that can give me hints on what the problem is, it's the complete lack of them that's driving me mad! The following code works, just not as it should! The annotations for checking that the input is not null or lesser then 3 character long just never runs. Nor are they giving any kind of error when the project is being deployed or when the name variable is being written to. public

JSF/Hibernate NotBlank validation

只谈情不闲聊 提交于 2019-12-02 17:59:52
问题 I have a simple JSF+RichFaces form with some fields and obviously a backing bean to store them. In that bean all the necessary properties have validation annotations (jsr303/hibernate), but I can't seem to find an annotation which would check if the property (String) is blank. I know there's a @NotBlank annotation in spring modules, but JSF doesn't support spring validation. Is there any easy way to check it or should I write my own annotation? @Edit: I already tried @NotNull and @NotEmpty

JSR 303 Bean Validation + Javascript Client-Side Validation

懵懂的女人 提交于 2019-12-02 17:25:50
What is the best way to perform client-side form validation using Javascript (with minimal code duplication) when using JSR 303 bean validation on the server side? I'm currently using Spring 3 and the Hibernate Validator . I would suggest that you look at Spring JS, which relies heavily on Dojo. A tutorial can be found here . Easiest way for yourself to start playing with it is to download Spring Roo , create the petclinic sample application with one of the example-scripts (this takes you 5 minutes) and then play around with how the javascript is integrated. Spring Roo creates an app with the

Initializing Spring bean from static method from another Class?

旧城冷巷雨未停 提交于 2019-12-02 17:25:31
I was trying to create Hibernate Validator bean, and run into this problem creating a bean from static factory method in another Class. I found a Spring way to get my Validator bean initialized (solution at the bottom), but the problem itself remains unsolved. Validator is used as example case here. This is how I create the Validator instance in Java import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); This is how I

hibernate unique key validation

别来无恙 提交于 2019-12-02 16:48:32
I have a field, say, user_name , that should be unique in a table. What is the best way for validating it using Spring/Hibernate validation? One of the possible solutions is to create custom @UniqueKey constraint (and corresponding validator); and to look-up the existing records in database, provide an instance of EntityManager (or Hibernate Session )to UniqueKeyValidator . EntityManagerAwareValidator public interface EntityManagerAwareValidator { void setEntityManager(EntityManager entityManager); } ConstraintValidatorFactoryImpl public class ConstraintValidatorFactoryImpl implements

javax.validator with spring component

你离开我真会死。 提交于 2019-12-02 16:00:56
问题 I use javax.validation with Spring. In my test (groovy) I explicitly create validator. import javax.validation.Validation import javax.validation.Validator import javax.validation.ValidatorFactory ValidatorFactory factory = Validation.buildDefaultValidatorFactory() Validator validator = factory.getValidator() when: Set<ConstraintViolation<User>> constraints = validator.validate(entity) My validator in java public class EntityDynamicValidator implements ConstraintValidator<SomeConstraint,

How to get the Invalid value in the error message while using custom validator in spring?

不打扰是莪最后的温柔 提交于 2019-12-02 12:59:01
I having trouble in showing Invalid value in the error message I have messages.properties file as follows ProductId.exists.custom.validator.validation = A product already exists with product id ${validatedValue}. and following is my custom validtor interface @Target( { METHOD, FIELD, ANNOTATION_TYPE }) @Retention(RUNTIME) @Constraint(validatedBy = ProductIdValidator.class) @Documented public @interface ProductId { String message() default "{ProductId.exists.custom.validator.validation}"; Class<?>[] groups() default {}; public abstract Class<? extends Payload>[] payload() default {}; } Here is

javax.validator with spring component

删除回忆录丶 提交于 2019-12-02 09:54:16
I use javax.validation with Spring. In my test (groovy) I explicitly create validator. import javax.validation.Validation import javax.validation.Validator import javax.validation.ValidatorFactory ValidatorFactory factory = Validation.buildDefaultValidatorFactory() Validator validator = factory.getValidator() when: Set<ConstraintViolation<User>> constraints = validator.validate(entity) My validator in java public class EntityDynamicValidator implements ConstraintValidator<SomeConstraint, Entity> { private GroupService groupService; // This constructor is required, see the link bellow. public