LSPS documentation logo
LSPS Documentation
Validation of Multiple Components

Required result:

A forms::form component becomes invalid as part of front-end validation when some components hold a certain combination of values: in the example, a Text Field will be valid only if another Text Field contains a correct value and if the combination of the values of the fields is valid.

  1. Create a form with two Text Fields.
    group_validation_form.png
  2. Set field IDs, for example, to a and b.
  3. Define a method on the form that add custom error messages to components when they contain invalid values:
        //rules for validation of the fields:
        private void validateGroup(){
     
          //error message for field a:
          def String error1 := (a.getValue() == "1" and b.getValue()== "1") ? "Values must not equal 1." : null;
          //error message for field b:
          def String error2 := (b.getValue()== "3") ? "b value must not equal 3." : null;
          def String all := joinErrors(error1, error2);
     
          //setting the errors as custom error messages on a:
          if !all.isBlank() then
             a.setCustomErrorMessage(all);
          end
        }
     
        //concatenate errors from components:
        private String joinErrors(String... errors) {
           def String concatenated := join(errors, "<br>");
           concatenated.isEmpty() ? null : concatenated;
        }
    
  4. Call the method whenever a value is changed on either of the fields or whenever applicable. click.
    //Init on text fields:
    c.setOnChangeListener({  e ->
       validateGroup()
    })