Questions tagged [bean-validation]

Bean Validation, previously commonly called just "JSR-303", is an annotation based validation framework for javabean properties and parameters of arbitrary methods. Hibernate Validator is the reference implementation and the most widely used one.

Online resources

1913 questions
261
votes
15 answers

Cross field validation with Hibernate Validator (JSR 303)

Is there an implementation of (or third-party implementation for) cross field validation in Hibernate Validator 4.x? If not, what is the cleanest way to implement a cross field validator? As an example, how can you use the API to validate two bean…
Bradley Dwyer
  • 8,102
  • 5
  • 32
  • 28
131
votes
5 answers

How can I validate two or more fields in combination?

I'm using JPA 2.0/Hibernate validation to validate my models. I now have a situation where the combination of two fields has to be validated: public class MyModel { public Integer getValue1() { //... } public String getValue2()…
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
131
votes
3 answers

In Hibernate Validator 4.1+, what is the difference between @NotNull, @NotEmpty, and @NotBlank?

I can't seem to be able to find a summary that distinguishes the difference between these three annotations.
Rick Hanlon II
  • 20,549
  • 7
  • 47
  • 53
128
votes
6 answers

JSR 303 Validation, If one field equals "something", then these other fields should not be null

I'm looking to do a little custom validation with JSR-303 javax.validation. I have a field. And If a certain value is entered into this field I want to require that a few other fields are not null. I'm trying to figure this out. Not sure exactly…
Eric
  • 1,383
  • 2
  • 9
  • 5
82
votes
22 answers

Annotations from javax.validation.constraints not working

What configuration is needed to use annotations from javax.validation.constraints like @Size, @NotNull, etc.? Here's my code: import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Person { …
Darshan Patil
  • 1,049
  • 2
  • 13
  • 19
72
votes
2 answers

kotlin data class + bean validation jsr 303

I'm trying to get Kotlin working with jsr 303 validation on a spring-data-rest project. Given the following data class declarartion : @Entity data class User( @Id @GeneratedValue(strategy = javax.persistence.GenerationType.AUTO) var…
pellenberger
  • 813
  • 1
  • 6
  • 7
53
votes
5 answers

JSR 303: How to Validate a Collection of annotated objects?

Is it possible to validate a collection of objects in JSR 303 - Jave Bean Validation where the collection itself does not have any annotations but the elements contained within do? For example, is it possible for this to result in a constraint…
cam
  • 681
  • 1
  • 6
  • 7
51
votes
4 answers

How to disable Hibernate validation in a Spring Boot project

I have a spring boot project that has a CrudRepository, an Entity and a Controller. I am basically trying to persist an entity based on the data passed to the Controller. To do this, I am using spring-boot-starter-jpa. My Entity is annotated with…
Erin Drummond
  • 5,347
  • 6
  • 35
  • 41
45
votes
7 answers

JSR 303 Bean Validation + Javascript Client-Side Validation

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.
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
44
votes
6 answers

Is there an implementation of JSR-303 (bean validation) available?

I know there are non-standard frameworks such as commons-validator, and hibernate validator. I wanted to know if someone knows an implementation of the official standard.
flybywire
  • 261,858
  • 191
  • 397
  • 503
43
votes
2 answers

Bean validation size of a List?

How can I set a bean validation constraint that a List should at minimum contain 1 and at maximum contain 10 elements? None of the following works: @Min(1) @Max(10) @Size(min=1, max=10) private List list;
membersound
  • 81,582
  • 193
  • 585
  • 1,120
42
votes
2 answers

Initializing Spring bean from static method from another Class?

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…
Spaideri
  • 514
  • 1
  • 5
  • 10
41
votes
6 answers

Hibernate Validation of Collections of Primitives

I want to be able to do something like: @Email public List getEmailAddresses() { return this.emailAddresses; } In other words, I want each item in the list to be validated as an email address. Of course, it is not acceptable to annotate…
scrotty
  • 833
  • 2
  • 10
  • 17
41
votes
5 answers

Unique constraint with JPA and Bean Validation

I'd like to have a @Unique constraint with Bean Validation, but that is not provided by the standard. If I would use JPA's @UniqueConstraint I wouldn't have a unique validation and error reporting mechanism. Is there a way to define @Unique as a…
deamon
  • 89,107
  • 111
  • 320
  • 448
41
votes
4 answers

Hibernate @NotEmpty annotation equivalent in Javax.package or alternative

Is there a way to implement @NotEmpty hibernate validation without writing custom validation? javax.validation package does not contain this annotation. Only @NotNull. But it does not validate for Non-null but empty values. So I would like to see an…
Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
1
2 3
99 100