Form Validation

Works with any validation attribute. Here’s an example of validating a pattern with a server-side regex.

Here’s a link to the issue: https://github.com/ironmansoftware/universal-dashboard/issues/417

1 Like

Nice! While I see this a super helpful in eliminating dozens of IF statements in my logic, would i be able to use custom error messages, like “Oops, that’s not a valid phone number” rather than a bunch of syntax about regex pattern validation? End users would be kinda confused when they made a mistake, if that makes sense.

Totally agree and I’ll make sure that’s something that get’s implemented as part of this. It’s not currently built into validation attributes in PowerShell but maybe there is something else we could do here.

Maybe a custom attribute would work? Might not work if you have multiple validation attributes.

param(
     [ValidationErrorMessage(''Oops, that's not a valid email address!")]
     [ValidatePattern('.*@.*')]
     $Email
)

I think it will be easier for UDInputs using New-UDInputField as we could provide parameters for messages\validation pretty easily.

Any other recommendations on how’d you envision that working would be great.

2 Likes

I have this hooked up. :partying_face:

image

2 Likes

That’s pretty much exactly what I had in mind, nice work!

Something like this would work. ValidateScript is almost always better than ValidatePattern as you can create your own error messages.

function Test-ValidateScript {
    [cmdletbinding()]
    param (
        [Parameter()]
        [ValidateScript({
            if ($_ -eq 'localhost') {
                $true
            }
            else {
                throw "Bad computer name"
            }
        })]
        [string] $ComputerName
    )

    $ComputerName
}
3 Likes