Step 8: Validation in dialogs
Now that the most important features are done, we can work on the details. One of them is user input validation. Validation is a way to tell the user whether the input fits the domain model logic. You can read more about validation in linkki in the chapter Validation of the linkki documentation.
As a first step, the fields that are required should be visually marked as below in the edit dialogs.
This is quickly done. All we have to do is to switch to the corresponding PMO classes and set the required
property in the annotations of the mandatory fields. We will add this property to the field name
in the details dialog and to all the fields in the address dialog.
@UITextField(position = 10, label = "Name", modelAttribute = "name",
required = RequiredType.REQUIRED)
public void name() {
// model binding
}
Note that specifying the RequiredType
will only display the asterisk, it does not prevent the user from leaving the field empty, nor will it display the validation rule that is violated by that field. This is what we want to achieve next.
Validations typically have a justification in the domain model. If an attribute is mandatory for example, this information should be handled by the implementation in the domain model, so that all applications that use the same model follow the same logic. In our example, all domain model classes including BusinessPartner
, Address
and ContactInfo
all implement a method validate()
. This method returns a list of validation messages that contain the justification, as well as which attributes of which objects they refer to. For us, the only thing left to do is to transfer these messages to the fields in the UI.
In linkki, displaying and distributing validation messages are handled by so-called ValidationServices
. Whenever we create a dialog with the help of a PmoBasedDialogFactory
, it is possible to pass a ValidationService
that is used in all dialogs that are created by that factory. To add a ValidationService
to the edit partner dialog in the DetailPage
, we can simply use the validate
method of the partner to edit as a ValidationService
when creating the PmoBasedFactory
in DetailPage
.
public void editPartner() {
...
ValidationService validationService =
() -> businessPartner.validate();
PmoBasedDialogFactory dialogFactory =
new PmoBasedDialogFactory(validationService);
...
OkCancelDialog dialog = dialogFactory
.newOkCancelDialog("Edit Partner",
okHandler,
new PartnerDetailsDialogPmo(businessPartner));
...
}
Similarly, the same step has to be applied when creating the dialog in AddMenuItem
, ContactPage
and AddressPage
. Note that for ValidationServices
to work correctly, it is important to make sure that all UI annotations on the fields define the annotation element modelAttribute
correctly. This is crucial for linkki to be able to distribute the messages to the right fields.