Skip to content

Bean validation (JSR 303)

Antoine Mischler edited this page Aug 30, 2017 · 5 revisions

Bean validation

FXForm2 uses the JSR-303 Bean Validation API for JavaBean validation.

FXForm2 is independent of the JSR-303 implementation. FXForm2 tries to load a ValidatorFactory using the Validation.buildDefaultValidatorFactory().

If no implementation can be found in the classpath, bean validation won't be enabled.

If you are using maven you can easily provide the hibernate implementation by adding the following dependency to your project pom:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.3.Final</version>
</dependency>

When the user edits the form, FXForm2 will check the edited field against the new input value. If some constraints are violated, they will be displayed to the user and the model value won't be updated.

Defining constraints on JavaFX properties

Until now no library implements constraints for JavaFX properties such as StringPropertyor IntegerProperty.

However, you can easily use Property-level constraints for these fields.

Example:

public class DemoObject {

    private StringProperty mail = new StringProperty();

    private IntegerProperty age = new IntegerProperty();

    @Email
    public String getMail() {
        return mail.get();
    }

    @Min(value = 5)
    public int getAge() {
        return age.get();
    }

}

For more informations about validation with FXForm2 (class level constraints, strict and warning constraints) see this blog post : http://blog.dooapp.com/2013/11/fxform-two-releases-and-lot-of-new.html