Java – integrate hibernate validator with wicket
•
Java
I have some entities, and some fields are annotated with hibernate validator, such as:
@Entity public class MyEntity { @Pattern(regexp = "[A-Z,0-9]{3,}") @Column private String key; @Range(min = 1,max = 999) @Column private Integer year; // [...] }
Is there any way to use the hoose validator annotation in wicket components (such as forms)?
Solution
I found my own answer:
Hibernate validator is just an implementation of Java Bean validation (javax. Validation, JSR - 303)
From wicket 6.4 Starting from 0, java bean validation is supported in wicket
1) Add wicket bean validation for your project. For example, use Maven:
<groupId>org.apache.wicket<groupId> <artifactId>wicket-bean-validation</artifactId> <version>0.5</version>
2) Add propertyvalidator to your wicket fields, for example:
form.add(new TextField("key",new PropertyModel(myModel,"key")) .add(new PropertyValidator())) form.add(new TextField("year","year")) .add(new PropertyValidator()))
The rest made wicket and Hibernate validator for you
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码