LSPS documentation logo
LSPS Documentation
Validating Form Data

You can validate data in your form with the following mechanisms:

  • automatically triggered validation that is performed as part of the event processing directly on listeners as one of the following:
    • Validators: set of expressions that check if values in the form are valid and return a String with an error message if they are not

      The String is displayed as an error message either on the current component or the component defined in the Error placement property.

    • Data validation: expression that returns a set of constraint violations rendered automatically on the components with the violations
  • explicitly triggered validation that you can perform from the Handle expressions of your listeners

    You will need to collect a set of constraint violations, typically, by calling the validate() function on the respective Record, and then call the showConstraintViolations() function to display the violations in the form. For further information, refer to the documentation on Record validation.

Defining Validation in Listener Handle

When validating a Record in the Handle expression of a Listener, do the following:

  1. Collect a list with constraint violations; typically you will use the validate() function on the record or property.
  2. Call showConstraintViolations() function to display the error messages of the violation on the respective form components.
validationinhandle.png
A record validated as part of a listener's Handle

Defining Automatic Form Validation

You can define both the validators and the data validation on listeners on their Basic tab:

  • Validators with validator expressions that return a String when they fail, for example, if searchVar!=null and length(searchVar)>0 then null else “Provide search string!” end.

    The String is displayed as an error message either on the current component or the component defined in the Error placement property.

  • Data Validation as an expression that returns a list of ConstraintViolations: to obtain such a list, use the validate() function.

    If you need to display a constraint violation on a particular component, create a constraint violation with record or property set to the same binding as your component; for example, [new ConstraintViolation(payload -> null, record -> selectedRecord, property -> LookupValue.displayName, guid -> null, id -> null, message -> "Incorrect value.")]

The automatic validation passes only if:

  • all validator expressions from the entire form return null and
  • data validation expressions from the entire form do not return any validation constraint violations.
validatingwithvalidatecallandvalidator.png
Listener with validators and data validation

Validation of non-ValueChangeEvents

The validation of ValueChangeEvents is governed by their own validation mechanism: they are executed always when their validation passes regardless of other failed validations on other listeners. Also their Execute if other validations failed setting is not taken into account.

In addition, validity of ValueChangeEvents has no impact on the general validity of non-ValueChangeEvents.

Consider the form below: the ISBN text box is in non-immediate mode and its ValueChangeListener defines a validator for the entered text: it must be at least 18 characters long. The Submit button has an ActionListener with the Execute if other validations failed property set to false and executes submit (the Submit property is selected).

formIgnoringVCLFail.png
Book Create Form
If the user enters less than 18 characters in the ISBN text box and hits Submit, the invalid value is persisted and the form is submitted: the ActionListener on the Submit button ignores that the validation of the ChangeValueListener failed.

Impact of Listener Validations on their Execution:

Listener Listener Type Listener Validation OutcomeExecute if Other Validations FailedExecuted
A Non-ValueChangeEventfail false no
B Non-ValueChangeEventfail true no
C Non-ValueChangeEventpass true yes
D Non-ValueChangeEventpass false no
E ValueChangeEvent fail true no
F ValueChangeEvent pass false yes
G ValueChangeEvent fail false no

We assume that the listeners in the table exist in one context:

Listener A and B
Validation on Listeners A and B failed and hence they are not executed regardless of their Execute if Other Validations Failed setting.
Listener C
Validation on Listener C passed and its Execute if Other Validations Failed setting is true; hence it ignores the failed validation on A and B and it is executed.
Listener D
Validation on Listener D passed. However, since its its Execute if Other Validations Failed setting is set to false and validation on other non-ValueChangeListeners (A, B) failed, it is not executed.
Listener E
Listener E is a ValueChangeListener: its validation failed and hence it is not executed. The Execute if Other Validations Failed setting is ignored.
Listener F
Listener F is a ValueChangeListener: its validation passed and hence it is executed. The Execute if Other Validations Failed setting is irrelevant.
Listener G
Listener G is a ValueChangeListener: its validation failed and hence it is not executed. The Execute if Other Validations Failed setting is irrelevant.

Handling an Event When Validation Failed

If necessary, you can force the system to execute a particular listener even after the total validation had failed: Ignoring failed validations on other listeners allows you to perform an action even if the form contains invalid values.

It is then enough that the validation on that particular listener passes for the event to be handled: failed validations on other listeners are ignored.

This behavior is activated by the Execute if other validations failed property of the listener.

Filtering Validation Errors

When using validation of constraints on form components, by default, validation errors are displayed on the form component that contains the value with the error. However, you can define explicitly additional validation errors displayed on the component or hide such validation errors on the component:

  • Excluding validation errors is intended for filtering of errors, so that only a subset of the errors is displayed on the form component.
  • Including validation errors defines additional validation errors displayed on the component.

    These could be errors that would otherwise remain hidden. For example, when editing multiple record fields, which would result in an overall record constraint violation, the error for the entire record would not be displayed since there is no form component that references the entire record.

You can define the excluded or included validation errors on the Validation Errors tab of the form component properties.

excludeValidationError.png
Including a Validation Error on a Form Component
If you want to ignore invalid data in a form and enforce processing of an event, select the Execute even if invalid components setting for the given listener: the listener will be always executed as part of the next event processing.

Validating Initialized Forms

To validate initialized forms, design the forms as follows:

  1. On the form, define an InitListener which fires an ApplicationEvent.
  2. Define an ApplicationEventListener that listens for the ApplicationEvent.
  3. On the ApplicationEventListener, define the validation expression.