for the new form tool.
anyone have a good method for adding a Not in (x,y,z) to text validation constraint.
For example form to insert into DB DeviceName but DeviceName must be unique.
Have custom.Device = list of device names in DB [device01, device02, device03]
then form field will show invalid if duplicate if in that list.
Use the pattern validation, constructing a regex that uses 'negative lookahead' to confirm that the entire string supplied by the user does not match one of your known device names:
https://regex101.com/r/S04glN/1
The assembled regex, which looks like:
^(?!(device01|device02|device03)$).*$
Can be generated by an expression populated by an array of strings in this.custom.devices
:
stringFormat("^(?!(%s)$).*$", groupConcat({this.custom.devices}, "|"))
And then you can define a custom error message for the pattern mismatch state:
3 Likes
Is there a practical limit as to how long that list can be? I suppose it depends…
Seat of the pants answer without any real testing to back it up: I wouldn't put more than ~hundreds of items in there.
The "nice" thing about the way the form component works is you're passing that regex compilation + parsing load entirely onto the "user agent", aka wherever the Perspective session is actually running, so you're not killing your gateway asking it to parse a bunch of repetitive regexes.
The not-nice thing in this case is that regex validation is very CPU bound, so a sufficiently complex regex and a sufficiently weak client device may noticeably struggle. You're also at the mercy of the user agent - it's entirely possible that a browser might choose to bail out of a long running regex validation entirely or do something else unexpected. As ever - client side validation is a nicety, but anything truly important must be validated on the server where you control the code.
2 Likes