I have some entities with some fields annotated with hibernate-validator annotations like:

@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 make use of thoose validator annotations in wicket components (like forms)?

有帮助吗?

解决方案

I found the answer for myself:

hibernate-validator is just an implementation of java-bean-validation (javax.validation, JSR-303).

Since Wicket 6.4.0 there is a support in wicket for java-bean-validation.

1) add wicket-bean-validation to you project, e.g with maven:

<groupId>org.apache.wicket<groupId> 
<artifactId>wicket-bean-validation</artifactId> 
<version>0.5</version>

2) Add PropertyValidator to your wicket-fields, like:

form.add(new TextField("key", new PropertyModel(myModel, "key")) .add(new PropertyValidator()))
form.add(new TextField("year", new PropertyModel(myModel, "year")) .add(new PropertyValidator()))

The rest do wicket and hibernate-validator for you ...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top