K2 have introduced a concept called Unbound rules post 4.5 release.
Unbound rules are nothing but a set of rules which are not bound to any control or event. They can be used as a common method which can be called multiple times across different events instead writing them individually in each event.
Also, as you already know that Nested for each looping is not possible in K2 smart forms, we can achieve it using unbound rules.
To-day I would like to explain how we can create unbound rules and use them across multiple events and then I will show you how we can achieve Nested for each loop using them
Let me start with creating an unbound rule and use it across multiple events.
Let’s assume we have a smart form with some controls along with 3 buttons Previous, Save, Next and on each button click we need to validate the form.
For this requirement we will create an unbound rule we use it across all the 3 button clicks.
Create a View as shown below
All the fields here are mandatory for each button click
Now navigate to Rules tab and click on Add Rule and provide a name to unbound rule as you like. Here in my Case it is ValidateView as shown highlighted below
Now Navigate to Conditions Tab below and select an advanced condition is true and click on Green link to add the Conditions
Now add the validation Conditions as shown below and click OK to create advanced Condition.
Now I would like to show a message when any of this validation fails.
So navigate to Actions tab below and select show a message and click configure link and set the details as shown below
Added HTML formatting tags, just to make this Validation message pop – up look pretty J
Now we need to stop the execution when this validation gets failed, so we need to add one more rule with name Stop rule execution which will be available in Actions tab. You just need to add this as shown below, there is nothing to configure in this rule.
Now the unbound rule is ready and we can start using this anywhere we required.
So let’s start configuring Save Button rule, click Add rule and from Events tab select when a control on the view raises an event and then click select control and then select the button you wish to configure the rules for.
Now I will select save button and then move to Actions tab and select Execute another rule and now click the green Configure link and select the unbound rule that you have created above.
Now after this rule we can add our logic to save the data or anything we want to do, which is explained in below screen shot. The rules that are written under this will be executed only when the above rule which validateView is successful.
Now as we did for save button we have to configure for other 2 buttons and use the same unbound rules for validating the controls. Once you configure all the rules the designer should look like something like below
Now Save this view and add this to a form (or you can run it directly too) and test it.
When I click on Save button it throws me a validation message as mandatory fields are empty, the same message will be shown when u click on any button if the mandatory fields are empty.
Now let me put some data into fields and click on Save again and see the result in below screen shot.
As I have entered all mandatory data the save rule executes successfully and shows me a success message.
So this how we can make use of unbound rules as per our requirement and make our code much efficient.
Now achieving Nested For Loop see it here NestedForLoopUsingUnBoundRules