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
23
votes
7 answers

Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'

I would like to store birthdate so I chose date at MySQL, when I create my entities based in my database, it turns out like this: import java.util.Date; // ..code @NotNull(message="fill you birthdate") @Temporal(TemporalType.DATE) …
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
22
votes
2 answers

Kotlin data class and bean validation with container element constraints

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) This does not have any effect. Any…
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
22
votes
3 answers

How do I validate that the @RequestParams are not empty?

I have a calculator service that gets the operation type, num1 and num2 from the user. I need to validate that the user actually inputs these values and doesn't just leave it blank. @RequestMapping(value = "/calculate") @ResponseBody public…
Saakina
  • 275
  • 1
  • 2
  • 9
22
votes
2 answers

Can I change the property path in a ConstraintValidator for Method arguments?

If you are familiar with the Bean Validation Framework you know that you cannot get the name of a method argument. So if you do a @NotNull constraint on the first argument of a method and the validation fails the getPropertyPath will be something…
robert_difalco
  • 4,821
  • 4
  • 36
  • 58
22
votes
4 answers

Multiple Regex @Pattern's for 1 Field?

I was attempted to apply multiple @Pattern annotations to a single field: @Pattern(regexp = "(?=.*[0-9])", message = "Password must contain one digit.") @Pattern(regexp = "(?=.*[a-z])", message = "Password must contain one lowercase…
Chris Harris
  • 1,329
  • 4
  • 17
  • 28
22
votes
4 answers

How to handle Resource validation in REST webservices?

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…
miguelcobain
  • 4,734
  • 4
  • 32
  • 45
21
votes
3 answers

What is the difference between ConstraintViolationException and MethodArgumentNotValidException

When I am validating bean using @Valid annotation in javax.validation, for some objects I am getting ConstraintViolationException and for some I am getting a MethodArgumentNotValidException. I understand that, if I validate anything in @ResponseBody…
JOHND
  • 2,597
  • 6
  • 27
  • 35
21
votes
2 answers

How do I construct a ConstraintViolationException in Bean Validation 1.0?

I am puzzled by the javax.validation API. I am writing a simple test to understand it: Sample sample = new Sample(); Set> violations = validator.validate(sample); if (!violations.isEmpty()) { // Eclipse refuses to let…
Raylite3
  • 837
  • 2
  • 11
  • 22
20
votes
3 answers

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

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…
javagruffian
  • 607
  • 2
  • 7
  • 13
20
votes
2 answers

How do I use @Valid with Spring MVC's @RequestBody parameters?

I'm having an issue with using @Valid on a parameter to a handler method on my @Controller. My code looks like this: @RequestMapping(value=BIBBLE_BOBBLE_URI_PATTERN + "/widgets",…
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
20
votes
3 answers

Kotlin Spring bean validation nullability

In my Spring application that is built with Kotlin I would like to use bean validation on a data class that looks like this. data class CustomerDto( @field: NotBlank val firstName: String, @field: NotBlank val lastName: String) On…
jgeerts
  • 641
  • 8
  • 17
20
votes
6 answers

Validate Mobile Number using Hibernate annotation

I have a entity called User and I want to validate a mobile number field The mobile number field is not mandatory it can be left blank but it should be a 10 digit number. If the user enters any value less then 10 digits in length then an error…
Ankur Raiyani
  • 1,509
  • 5
  • 21
  • 49
19
votes
5 answers

Convert JSR-303 validation errors to Spring's BindingResult

I have the following code in a Spring controller: @Autowired private javax.validation.Validator validator; @RequestMapping(value = "/submit", method = RequestMethod.POST) public String submitForm(CustomForm form) { …
Anton Moiseev
  • 2,834
  • 4
  • 24
  • 30
18
votes
7 answers

hibernate unique key validation

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?
nidhin
  • 6,661
  • 6
  • 32
  • 50
18
votes
3 answers

JPA @Size annotation for BigDecimal

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;
user1191027