API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Making Reusable Validation Formulas In ND6
With the complete rewrite of the formula language in ND6, many new features have been added. Breaking Par's tips will explore many of the new features over the coming months. One of the ones we're most excited about is actually a combination of three new functions that are going to allow translation/validation to be copied/pasted without having to change any code at all.

It's easiest to show this with an example. In a form, we created a table for inputting matching fields of information. The first field was a server name and the second field was a directory. If you enter a server, you have to enter a directory and vice versa. But both could be left blank. This is pretty easy to implement. But when you want to have several of these field pairs, you copy and paste the field. Notes automatically appends "_#" to the end of the field name. Knowing this, we can create the translation and validation formulas on the editable fields in such a way that, once copied/pasted, they don't need to change at all and will still validate the way we want.

First, create a field called Server. In the input translation, we want to trim the value. Normally, you would use this code:
@Trim(Server)

But, once the field is copied and pasted, Notes will create Server_1 and we would need to change the translation formula. If we make the translation formula to be something generic, like:
@Trim(@ThisValue)

then copying/pasting can be done without having to change the formula. The formula @ThisValue is new to ND6.

There is no validation on this field - we'll do the validation in the "matching" field. Create a field called Directory. Again, we want to trim the value during translation. So the translation formula should be:
@Trim(@ThisValue)

Beginning to see a pattern with the translation formula? You should put that translation formula into every text field in ND6 that is required. It's an easy copy and paste. And with the type-ahead feature built in to the ND6 designer, it's actually pretty easy to type (although it's easier to paste).

The input validation is really where these new functions come into play. First we need to find the "matching" server name. There's a new formula called @ThisName that gives you the current field name. Knowing that when we copy and paste fields Notes puts "_#" at the end, we can grab the current suffix from the field name. This will allow us to get the matching server field (the one with the same suffix). To get the value from that matching server field, we make use of the new function @Eval which evaluates a formula. So here is our input validation formula for the Directory field:

Suffix := @RightBack(@ThisName; "_");
PairServer := @Eval("Server_" + Suffix);
@If(PairServer = "" & @ThisValue = ""; @Success; PairServer = "" & @ThisValue != ""; @Failure("You should not enter a directory without entering a server!"); PairServer != "" & @ThisValue = ""; @Failure("If you enter a server name, you must enter in the directory name!"); @Success)

The value PairServer contains the matching server name. If the matching server name is blank and the current field value is blank, everything is fine. If one is blank and the other is not, then do not allow the save (give the appropriate reason). Otherwise, they are both filled in and the validation passes.

Now, you can copy and paste the Server and Directory fields as many times as you want. You don't need to update any formulas after pasting - the formulas are generic enough that they will validate correctly.