Regular Expressions in Smartforms Rule


Badge +2

Hi,

 

We're a new K2 user so forgive me if the answer to this is painfully obvious.

 

I was wanting to write a Smartforms rule which has an IF condition which compares a textbox control value against a regular expression (i.e. it's a search field which filters a list slightly differently depending on the format of the value).  Is it possible to use a reular expression match for smartform rules?  I thought it'd be an option within "Advanced Condition" but I can't find anything.

 

Thanks

Peter


5 replies

Userlevel 1
Badge +8

Hi Bugmeister

 

Use of regular expressions in smartforms is limited to validations. You can create or use an existing "pattern" (which is a regular expression) and assign this to a control. If the data in the control does not match the expression it will display a red outline. You can also use the "When the View/Form passes Validation" condition to check if the data in the control passes the validation (i.e. matches the pattern's regular expression). When configuring this condition you tick the "Validate" check box for the control.

 

I suggest for requirement you create a pattern with your regular expression and assign it to your textbox control. Then use the "When the View/Form passes Validation" as your IF condition. 

 

If you don't want the textbox to get a red outline when it fails the validation, you can create a data label, set its visible property to false and assign it an expression that contains the text box value. You can then do the validation on the data label - remember to untick the "Do not validate hidden, disabled or read-only controls, Views and tabs." option when configuring the validation condition.

Badge +2

Andrew,

 

Thanks for suggesting that quite nifty technique.  I am not sure if it'll work in my instance, as I was wanting a few RegEx checks in my rule, something like:

 

If <control> matches "ABCd{3}"

    execute view method ? with params ?

If <control> matches "XYZd{10}"

    execute view method ? with params ?

If <control> doesn't contain a value

    message

 

In the end though I could live with a simple "Starts With" check (not ideal, but it'll work well enough to get the logic flow happening).  

 

RegEx in the advanced conditions would be a nifty and flexible feature to have though.

 

Cheers

Peter

Userlevel 1
Badge +8

Bugmeister

 

There is also the the ability to use the Matches and Not Matches functions found in the Regular Expression folder in the Context Browser. You could for example create data labels for each of your conditions and set an expression on each one of these to evaluate the text box value against a regular expression using Matches. This function will return true if a match is found. Then when checking your conditions you can check the value of these data labels to see if they contain true. See attached image for example.


15621iA7AB46514CEB0679.png
Userlevel 1
Badge +8

Be sure to enclose your regular expression beteen ^ and $ tags so that it matches exactly the text entered. E.g. "^ABCd{3}$" will match "ABC123" but not "ABC1324"

Badge +2

Good technique, thanks :)

Reply