Skip to content

Validation

Mateu enforces standard Java Bean Validation annotations automatically. Rules defined in your ViewModel are reflected in the UI without any additional configuration.


import jakarta.validation.constraints.Size;
@NotBlank
String name;
@NotNull
ProductStatus status;
@Email
String 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

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:

  • @NotNull and @NotEmpty need a value. 0 and false count as values. An empty input, a missing value and an empty list do not.
  • @NotBlank also 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 @NotNull to 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.


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.


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)
);
}
}

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.


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 @Validation on 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:
@Button
public 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)