bean-validation

Bean-Validation 1.1 in Glassfish 4.0 - CDI Injection not working as intended

百般思念 提交于 2019-12-03 06:43:22
According to the Glassfish 4.0 wiki, Glassfish 4.0 should include JSR349 Bean Validation 1.1.: GF4 wiki link According to the JSR349 spec, CDI Injection should work out of the box: Bean Validation 1.1. CDI Integration So I changed my pom.xml accordingly: <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> <scope>provided</scope> </dependency> And tried injecting a CDI Bean into the ConstraintValidator: public class UniqueEmaiValidator implements ConstraintValidator<UniqueEmail, String> { @Inject private UserAccountService

Empty validatedBy in @Constraint

亡梦爱人 提交于 2019-12-03 06:03:52
I noticed that all built-in constraints have an empty value for the validatedBy parameter in @Constraint . i.e. @Constraint(validatedBy = {}) First, why are they allowed to have an empty value for validatedBy ? I thought you can leave it blank only for constraint composition that does not need addtional validation? Also, note that the Hibernate Validator can still find a validator implementation class for each built-in constraint, despite the validatedBy is empty, but if I leave the validatedBy blank for my constraint, my custom validator never gets picked up. Why is that? Thanks. Those built

JPA @Size annotation for BigDecimal

浪尽此生 提交于 2019-12-03 05:40:38
How do I use the @Size annotation for MySQL DECIMAL(x,y) columns? I'm using BigDecimal , but when I try to include the @Size max it doesn't work. Here is my code: @Size(max = 7,2) @Column(name = "weight") private BigDecimal weight; You could use the Hibernate Validator directly, and annotate your field with @Digits like so: @Digits(integer=5, fraction=2) @Column(name = "weight") private BigDecimal weight; borjab See this answer @Column(nullable= false, precision=7, scale=2) // Creates the database field with this size. @Digits(integer=9, fraction=2) // Validates data when used as a form

@Pattern for alphanumeric string - Bean validation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 05:16:49
I have a variable name in a bean. I want to add @Pattern validation to accept only alphanumeric. Currently, I have this one. @NotNull @Pattern(regexp = "{A-Za-z0-9}*") String name; But the error is Invalid regular expression. I tried [A-Za-z0-9] . But this is not working either. No errors though. It shows any valid input as failed. Do you try this pattern: ^[A-Za-z0-9]*$ or ^[A-Za-z0-9]+$ to avoid empty results. If you want to check that a string contains only specific characters, you must add anchors ( ^ for beginning of the string, $ for end of the string) to be sure that your pattern

Can you change an annotation message at run time?

江枫思渺然 提交于 2019-12-03 05:01:28
I'm trying to include a dynamic message in my annotation that changes the main body of the text based on the values that are found in the other variables that are passed to it. I set a default message, but when a certain indicator is set, I want to display a different message. Is this possible? Here's my annotation - @Target({TYPE, ANNOTATION_TYPE}) @Retention(RUNTIME) @Constraint(validatedBy = FieldMatchValidator.class) @Documented public @interface FieldMatch { Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; String first(); String second(); String third()

Initializing Spring bean from static method from another Class?

被刻印的时光 ゝ 提交于 2019-12-03 04:04:55
问题 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 =

hibernate unique key validation

纵然是瞬间 提交于 2019-12-03 03:17:45
问题 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? 回答1: 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

JSR-303 Bean Validation for enum fields

只愿长相守 提交于 2019-12-03 02:44:57
I have a simple bean with enum field public class TestBean{ @Pattern(regexp = "A|B") //does not work private TestEnum testField; //getters + setters } enum TestEnum{ A, B, C, D } I would like to validate testField using Bean Validation. Specifically I would like to make sure that only A and B values are allowed (for a particular calidation gropus). It seems that enums are not handled JSR 303 (I was trying to use @Pattern validator) or I am doing something in a wrong way. I am getting exception: javax.validation.UnexpectedTypeException: No validator could be found for type: packagename.TestEnum

Kotlin data class and bean validation with container element constraints

流过昼夜 提交于 2019-12-03 02:36:19
With Bean Validation 2.0 it is possible to also put constraints on container elements . I cannot get this to work with Kotlin data classes: data class Some(val someMap: Map<String, @Length(max = 255) String>) This does not have any effect. Any ideas? I created a repository with a sample project to reproduce the case: https://github.com/mduesterhoeft/bean-validation-container-constraints 来源: https://stackoverflow.com/questions/51085138/kotlin-data-class-and-bean-validation-with-container-element-constraints

JSR 303 Bean Validation + Javascript Client-Side Validation

眉间皱痕 提交于 2019-12-03 02:36:18
问题 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. 回答1: 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