Skip to content

RuleSupplier and ValidationSupplier

These interfaces let a ViewModel provide conditional UI rules and cross-field validations programmatically, as an alternative to using the @Rule annotation or bean validation constraints.


Interfaceio.mateu.uidl.interfaces.RuleSupplier

When a ViewModel implements RuleSupplier, Mateu merges the returned rules with any annotation-defined rules before sending them to the client. Rules are evaluated client-side: when the filter expression is truthy, the action is executed against the target field or action.

public interface RuleSupplier {
List<Rule> rules();
}
FieldTypeDescription
filterStringClient-side JS expression; rule fires when this evaluates to truthy
actionRuleActionWhat to do when the rule fires
fieldNameStringTarget field name
fieldAttributeRuleFieldAttributeWhich attribute of the field to modify
valueObjectStatic value to set
expressionStringClient-side JS expression whose result is set as the value
actionIdStringTarget action id (for RunAction)
resultRuleResultContinue to keep evaluating rules, Stop to halt
ValueDescription
SetDataValueSets a field attribute (hidden, disabled, required, style, …)
SetStateValueSets a field value in the component state
SetAppStateValueSets a value in the application-level state
SetAppDataValueSets a data value at application level
RunActionTriggers a named action
RunJSExecutes arbitrary JavaScript
SetAttributeValueSets an HTML attribute on the field element
SetCssClassAdds or removes a CSS class
SetStyleSets an inline CSS style

required, disabled, hidden, pattern, minValue, maxValue, minLength, maxLength, css, style, theme, errorMessage, description

@UI("/orders/{id}")
public class OrderForm implements RuleSupplier {
public String status;
public double discount;
@Override
public List<Rule> rules() {
return List.of(
Rule.builder()
.filter("state.status === 'closed'")
.action(RuleAction.SetDataValue)
.fieldName("discount")
.fieldAttribute(RuleFieldAttribute.disabled)
.expression("true")
.result(RuleResult.Continue)
.build(),
Rule.builder()
.filter("state.discount > 50")
.action(RuleAction.SetDataValue)
.fieldName("discount")
.fieldAttribute(RuleFieldAttribute.errorMessage)
.value("Discount cannot exceed 50%")
.result(RuleResult.Continue)
.build());
}
}

Note: RuleSupplier is evaluated once on the server and the rules are sent to the client for repeated evaluation. For purely server-side visibility/disabled control, use VisibilitySupplier or DisabledSupplier instead — they remove or disable fields at render time without sending client expressions.


Interfaceio.mateu.uidl.interfaces.ValidationSupplier

When a ViewModel implements ValidationSupplier, Mateu merges the returned validations with any bean-validation constraints before processing a form submission. Validations are evaluated server-side when an action is triggered.

public interface ValidationSupplier {
List<Validation> validations();
}
FieldTypeDescription
conditionStringServer-side SpEL expression; validation fails when this evaluates to true
fieldIdStringThe field to highlight when the validation fails
messageStringError message shown to the user
@UI("/orders/{id}")
public class OrderForm implements ValidationSupplier {
public LocalDate startDate;
public LocalDate endDate;
public double discount;
@Override
public List<Validation> validations() {
return List.of(
Validation.builder()
.condition("endDate != null && startDate != null && endDate.isBefore(startDate)")
.fieldId("endDate")
.message("End date must be after start date")
.build(),
Validation.builder()
.condition("discount < 0 || discount > 100")
.fieldId("discount")
.message("Discount must be between 0 and 100")
.build());
}
}