Press "Enter" to skip to content

Form Validation with Multiple Views


In this article I will demonstrate, how you can run forms validation with multiple views, showing only 1 message before submitting the form. This is one of the most frequent requirements from the customer. And even though this approach is applied inside K2 SmartStarters , not so many developers are aware of it.

Our sample application for Expense Claim Tracking has a Submit form, which contains several views to be validated. By default, the app wizard generates the form Validation Rule on the form level. However, if you create an application from scratch, it would be better to create view rules to be able to reuse them on multiple forms.

K2 Submiut form

Let’s imagine, that we need to validate the following:

  • Expense Claim Tracking Detail – required fields;
  • Claim Item Detail – at least 1 item should be added;
  • Workflow Approver – required field.

1. Create ValidationMessage parameter for all Views.

The ValidationMessage parameter will be used to store the Validation Message from every validated item.

K2 View parameter

2. Create Unbound rules on every view for validation

All the rules should follow the following pattern:

  • Clear ValidationMessage parameter;
  • Run Validation checks;
  • If the view does not pass the validation, then set the ValidationMessage parameter with the corresponding message.

This is an example of the Expense Claim Tracking Detail view validation:

K2 View Validation 1

For the Claim Item Detail view I need to create an expression, which counts a number of items in a List View. I hope, you are aware, how to do this, therefore, I will not go into details, but simply show you an example of the Claim Item Detail validation rule:

K2 View Validation 2

And a rule for Workflow Approver view looks like this:

K2 View Validation 3

3. Create a FormValidationMessage parameter on the form

K2 Form parameter

This parameter will be used to collect all Validation Messages from the view and check in general if the whole form passes validation or not.

4. Create the Form validation rule to rule them all.

The form validation rule should follow the following pattern:

  • Clear FormValidationMessage parameter
  • Execute all view validation rules.
  • Send all view ValidationMessage parameters into the FormValidationMessage one.
  • If the FormValidationMessage contains a value (i.e. at least 1 view did not pass validation);
    • show the full ValidationMessage to the user
    • Stop rule execution

Here is an example of the Form Validation rule:

K2 Form Validation rule

As you may have noticed, I added ‘<br><br>’ tags in the end of every validation message on the view and also made the message literal so that all messages are rendered as Html text. Thus I can make sure, that all the messages will show on separate lines in the form message. And now you can execute the rule before submitting the form. The Validation message will look like this:

K2 Form validation Message

And as you can see, all required fields are marked red and you see validation messages from all views in 1. This is a simple way to do form validation with multiple views and show a combined message to the user.

Leave a Reply

Your email address will not be published. Required fields are marked *

9 − two =