Validation
Mateu enforces standard Java Bean Validation annotations automatically. Rules defined in your ViewModel are reflected in the UI without any additional configuration.
Basic annotations
Section titled “Basic annotations”import jakarta.validation.constraints.Size;
@NotBlankString name;
@NotNullProductStatus status;
@EmailString contactEmail;
@Size(min = 2, max = 200)String description;
@Min(0)int stockCount;Mateu uses these annotations to:
- display validation errors next to the affected field
- prevent form submission when validation fails
- keep backend and UI validation consistent with a single source of truth
The constraints Mateu reads
Section titled “The constraints Mateu reads”Mateu reads these Bean Validation constraints, and only these: @NotNull, @NotEmpty,
@NotBlank, @Size, @Min, @Max, @Pattern, @Email, @Positive, @PositiveOrZero,
@Negative, @NegativeOrZero, @DecimalMin and @DecimalMax. Each one becomes a condition the
browser checks, and the server checks the same thing again before it runs the action, with the same
message. They are read the way Bean Validation reads them:
@NotNulland@NotEmptyneed a value.0andfalsecount as values. An empty input, a missing value and an empty list do not.@NotBlankalso refuses text that is only whitespace:" "fails," Ana "passes.- An empty optional value passes every bound (
@Min,@Max,@Size,@Pattern,@Email, the sign and decimal bounds). Add@NotNullto require it.
Any other constraint, such as @Past, @Future or a custom one, is not checked in the browser or
before an action runs. See Custom validators.
Triggering validation from an action
Section titled “Triggering validation from an action”A @Button or @Toolbar action, and a CRUD save, check the form before they run. Any other action
runs whatever the form holds, unless you ask for the check with @Action(validationRequired = true).
@Action(validationRequired = false) turns the check off for a button, for example a “Save draft”
button:
@Button@Action(validationRequired = true)public URI create() { String id = productRepository.create(name, status); return URI.create("/products/" + id);}If any field fails validation, the method is not called. Errors appear next to the affected fields. The server checks too, so a request that skips the browser’s check is refused with the same messages.
Full example
Section titled “Full example”import jakarta.validation.constraints.Size;
@UI("/products/new")public class NewProductForm {
@NotBlank(message = "Name is required") String name;
@NotNull(message = "Status is required") @Stereotype(FieldStereotype.radio) ProductStatus status;
@Size(max = 500) @Stereotype(FieldStereotype.textarea) String description;
@Min(value = 0, message = "Stock cannot be negative") int stockCount;
@Button @Action(validationRequired = true) public List<?> save() { String id = productRepository.create(name, status, description, stockCount); return List.of( new Message("Product created"), URI.create("/products/" + id) ); }
}Why this matters
Section titled “Why this matters”In traditional architectures, validation is duplicated:
- frontend validation (JavaScript)
- backend validation (Java)
With Mateu, validation is defined once in Java and applied in both places. There is no risk of the two diverging.
Custom validators
Section titled “Custom validators”A custom Bean Validation constraint (your own @Constraint annotation) is not picked up. Mateu
does not run a Bean Validation provider on a view’s form: it reads only the constraints listed
above, so a @ValidProductName is neither checked in the browser
nor before the action runs. The one exception is a CRUD record edited in a drawer
(editInDrawer()): its save runs the full validator, custom constraints included.
To check a rule of your own:
- If it can be written as an expression over the form, use
@Validationon the class. The browser checks it. See Validation annotations. - If it needs the server, check it in the action and refuse with a
UserFacingException. Its message is shown to the user as written:
@Buttonpublic Message save() { if (!productNames.isValid(name)) { throw new UserFacingException("Invalid product name", "Use letters, digits and spaces only"); } productRepository.rename(id, name); return new Message("Saved");}See it live in the Explorer: Validation (client + server)