Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
JFace Data Binding/JSR303BeanJFaceDatabindingValidation
Contents
Target
Work is underway to support JSR-303 Bean Validation with JFace Databinding Validators. This support was created after reading the Validating JFace Databinding with JSR-303 article.
You can find several plug-in projects from JSR-303 XDocReport Git which provides JSR-303 support for JFace Databinding Validators:
- org.eclipse.core.databinding.validation.jsr303 : JSR-303 support for JFace Databinding Validators source.
- org.eclipse.core.databinding.validation.jsr303.samples : JSR-303 support for JFace Databinding Validators sample with Java main.
- org.eclipse.core.databinding.validation.jsr303.samples.rcp : JSR-303 support for JFace Databinding Validators with Eclipse RCP (in an OSGi context).
JSR-303 support for JFace Databinding Validators
JSR-303 Overview
@Annotations for JSR-303
JSR-303 Bean Validation gives you the capability to declare with annotation your validation constraints in your Java Pojo model. Here an example with Person class to set validation constraints:
- "name" property as required.
- "email" property as email pattern
package org.eclipse.core.databinding.validation.jsr303.samples.model; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; public class Person { @Size(min = 1) private String name; @Size(min = 1) @Pattern(regexp = ".+@.+\\.[a-z]+") private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Those validation constraints can be used in any context :
- UI form could display errors when constraints are not respected. Ex : UI Text field email doesn't contains a well formatted email.
- ORM like Hibernate can use thoses constraints validation when Pojo model must be saved in the Database.
So JSR-303 gives a commons means to declare your constraint validation that you can used in several context of your application (in your UI form, in your DAO when Pojo must be saved, etc....)
Validator API for JSR-303
JSR-303 Bean Validation provides an API to validate property, value by using JSR-303 constraints validation declared with annotations. Here a sample code to validate the value "XXX" by using the annotations declared in the "email" property of the Person class :
... import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.ValidatorFactory; ... ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Set<ConstraintViolation<Person>> violations = factory.getValidator().validateValue(Person.class, "email", "XXX"); for (ConstraintViolation<Person> violation : violations) { System.err.println(violation.getMessage()); }
NOTE: this code works only if there is in the ClassPath an implementation of JSR-303 Bean Validation. If you have not an implementation, you will have this error :
Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
Otherwise, with Apache implementation, teh console will display:
must match the following regular expression: .+@.+\.[a-z]+
Validator Implementation for JSR-303
JSR-303 Bean Validation provides an API but not an implementation. It exists several implementation like:
- Hibernate Validator
- Apache Validator
Java main sample
The org.eclipse.core.databinding.validation.jsr303.samples