Validation examples?

Dec 9, 2011 at 10:08 PM

Are there any validation examples available for showui?

Specifically, if you have a bunch of textboxes that REQUIRE an entry, what is the most elegant way to achieve this in showui?





Oct 23, 2014 at 6:59 PM
Edited Oct 23, 2014 at 7:02 PM
I have the same question. Any examples?
Basically after Ok is clicked, a bunch of validators should run. Their result, if not ok, should be displayed in the form, ideally in red close to the failed field.
The form should remain open until all validations succeed or Cancel is clicked.
I am sure there is a simple way - all my ideas seem to be too awkward.


P.S.: I love ShowUI. Is it going to stay alive and ist it being continued?
Oct 26, 2014 at 7:27 AM
Edited Oct 26, 2014 at 7:34 AM
No idea if this is any good, but it does roughly what I wanted.:
Import-Module ShowUI

Function ErrorIfNull ($val, $lbl) {
    $fldName = $v.Name
    if ( -not $val.Text ) {
        $lbl.Content = "$fldName - Cannot be NULL"
        $lbl.Foreground = "red"
        return 1
    else {
        $lbl.Content = $fldName
        $lbl.Foreground = "black"  
        return 0     

$getCommandInput = UniformGrid -ControlName 'Get-InputForGetCommand' -Columns 2 {            
    Label -Name lblName -Content "Name"           
    TextBox -Name "Name"          
    Label -Name lblVerb -Content "Verb"          
    TextBox -Name "Verb"                        
    New-Button "Get Command" -On_Click {
        $error = 0 
        $error += ErrorIfNull $Name $lblName
        $error += ErrorIfNull $Verb $lblVerb
        if ( $error -eq 0 ) {                
            Get-ParentControl |            
            Set-UIValue -passThru |           
} -show
The validators can be extended or different ones be written outside the UI block. I am not to happy having to pass two objects to the validator and I am sure others have more elegant ways. would be nice to hear some feedback.
Apr 11, 2015 at 8:41 AM
You can do a little better than that, if you handle On_PreviewTextInput on the textbox, you can validate as the user types, and even disable an OK button, for instance. I've put it on my todo list to try and write an example of how to do this the way you would in WPF normally, but for now, I think you've got the idea.

Also, you might find this interesting, I use this for highlighting errors:

$control.Effect = New-DropShadowEffect -Color Red

and to clear it: