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.
Questions tagged [bean-validation]
1913 questions
16
votes
5 answers
How to perform JSF validation in actionListener or action method?
I have Bean validation working nicely in my application. Now I want to check that a new user does not choose a username that has already been chosen.
In the actionlistener I have the code that checks the database but how do I force the user to be…

Farouk Alhassan
- 3,780
- 9
- 51
- 74
16
votes
3 answers
How to make @PreAuthorize having higher precedence than @Valid or @Validated
I am using spring boot, and I have enabled the global method security in WebSecurityConfigurerAdapter by
@EnableGlobalMethodSecurity(prePostEnabled = true, order = Ordered.HIGHEST_PRECEDENCE)
And Below is my controller…

stevewho
- 163
- 1
- 5
16
votes
4 answers
Spring Bean Validation @Valid handling
I have created a Spring MVC REST service using Bean Validation 1.2 with the following method:
@RequestMapping(value = "/valid")
public String validatedMethod(@Valid ValidObject object) {
}
If object isn't valid, Tomcat informs me that The request…

Dormouse
- 1,617
- 1
- 23
- 33
16
votes
5 answers
Bean Validation: How can I manually create a ConstraintViolation?
I have a specific scenario where I can only check for violation conditions manually, at a later point in the flow.
What I want to do is throw a ConstraintViolationException, and provide a "real" ConstraintViolation object to it (when I catch the…

odedia
- 931
- 2
- 11
- 27
16
votes
3 answers
How do I get Spring MVC to invoke validation in a JUnit test?
I have a POJO called Browser that I've annotated with Hibernate Validator annotations.
import org.hibernate.validator.constraints.NotEmpty;
public class Browser {
@NotEmpty
private String userAgent;
@NotEmpty
private String…

Matt Raible
- 8,187
- 9
- 61
- 120
16
votes
1 answer
Java Bean Validation: GroupSequence with Class-Level Constraint
I have a bean class with multiple (custom) inner constraints and one class-level constraint. I'd like to validate the inner constraints before the class-level constraint. The code looks like this:
@GroupSequence({ Inner.class, NewSlotBean.class…

Hinton
- 2,320
- 4
- 26
- 32
15
votes
4 answers
How to properly throw MethodArgumentNotValidException
I'm trying to get the same result as when I use @Valid in object parameter from a Controller. When the object is invalid an exception (MethodArgumentNotValidException) is throw by my ExceptionHandlerController who has @RestControllerAdvice.
In my…

Mário Sérgio Esteves Alvial
- 269
- 1
- 6
- 14
15
votes
3 answers
Hibernate @OneToOne @NotNull
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…

Marty Pitt
- 28,822
- 36
- 122
- 195
15
votes
1 answer
Cross field validation with HibernateValidator displays no error messages
I'm validating two fields, "password" and "confirmPassword" on the form for equality using HibernateValidator as specified in this answer. The following is the constraint descriptor (validator interface).
package constraintdescriptor;
import…

Tiny
- 27,221
- 105
- 339
- 599
14
votes
1 answer
what to use, managed beans (backing beans) or entity beans?
I see a lot of examples marking beans as entity beans (@Entity) & named beans (CDI), so as to avoid creating 2 classes (managed bean & entity bean) and also to make use of Bean Validation so that validation can be performed on both client &…

Abdullah Shaikh
- 2,567
- 6
- 30
- 43
14
votes
3 answers
Dependency not autowired in JSR-303 Custom Validator
I have a Spring Boot app that contains an User class - all fields have standard JSR-303 annotations (@NotNull, @Size, etc.) and validation works fine.
However when I add a custom validation to User, I can't get a dependency injected into a custom…
user1348997
14
votes
3 answers
error: No validator could be found for type: java.time.LocalDate
I'm working on a project that uses bean validation (Hibernate Validator 5.1.3.Final). My bean has a attribute with the @Past annotation.
@Past(message = "A data deve estar no passado.")
private LocalDate dataAbertura;
But, when the validation…

Humberto Corrêa
- 789
- 1
- 6
- 17
14
votes
2 answers
Not showing error messages when validated using @valid(JSR-303) in Spring MVC
I have specified in dispatcher-servlet.
I am not using @InitBinder.
And I am using @valid annotation for validation in controller's method like
@RequestMapping(method = RequestMethod.POST, value = "new")
public…

hemal
- 153
- 1
- 1
- 7
14
votes
3 answers
JSR-303 Bean Validation for enum fields
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.…

Piotr Kochański
- 21,862
- 7
- 70
- 77
13
votes
4 answers
Good patterns for unit testing form beans that have annotation-based validation in Spring MVC
When using annotation based validation for a form bean, what is the best practice for unit-testing those beans in order to ensure that correct validations annotations are specified for each field?
For example, if you have:
public class MyForm {
…

Ashkan Aryan
- 3,504
- 4
- 30
- 44