New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce validation to Read-Variable properties and the form as a whole #728

Closed
AdamNaj opened this Issue Oct 21, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@AdamNaj
Member

AdamNaj commented Oct 21, 2016

@AdamNaj AdamNaj added this to the 4.2 milestone Oct 21, 2016

@AdamNaj AdamNaj self-assigned this Oct 21, 2016

AdamNaj added a commit that referenced this issue Oct 21, 2016

@michaellwest

This comment has been minimized.

Member

michaellwest commented Oct 22, 2016

I noticed that if there is an error in the validation scriptblock you have no way of viewing the output from the session.

@michaellwest

This comment has been minimized.

Member

michaellwest commented Oct 22, 2016

Here is an approach that I got working. The Validator appears to use a different session so any functions defined in the script won't load; therefore I created reusable functions. Any chance we can get the functions to auto-load?

$result = Read-Variable -Parameters `
    @{ Name = "username"; Value=""; Title="UserName"; Placeholder="User name goes here"; Mandatory=$true;
        Validator = { 
            Import-Function -Name Assert-UsernameRequirement
            $variable.Error = Assert-UsernameRequirement -Id $variable.Value
        }
    },
    @{ Name = "password"; Value=""; Title="Password"; Editor="password"; Placeholder="Password goes here"; 
        Validator = {
            Import-Function -Name Assert-PasswordRequirement
            $variable.Error = Assert-PasswordRequirement -Password $variable.Value
        }
    },
    @{ Name = "description"; Value=""; Title="Description"; Lines=3; Placeholder="User description";
        Validator = {
            Import-Function -Name Assert-NotNullOrEmpty
            $variable.Error = Assert-NotNullOrEmpty -Value $variable.Value
        }
    } `
    -Description "This Dialog shows how to provide a validator for values" `
    -Title "Password entry" -Width 450 -Height 450 -OkButtonName "Proceed" -CancelButtonName "Abort" -ShowHints
@AdamNaj

This comment has been minimized.

Member

AdamNaj commented Oct 22, 2016

You are right in the fact the session is a one-off throw away session that does not collect input.
It's still possible to Write-Log if needed, but I think I'll need to add error logging so it's easier to debug those little scripts.

For diagnostics purposes you can always append your message to the $variable.Error until you're happy with the results.

I think it's OK to ask people to import their own functions explicitly. In most cases the code is simple enough that it doesn't need a function and since section initialization happens every time a field is validated it could take a considerable time if we added all functions every time.

@michaellwest

This comment has been minimized.

Member

michaellwest commented Oct 23, 2016

You raise some good points @AdamNaj . Thanks for the clarification.

AdamNaj added a commit that referenced this issue Oct 23, 2016

@AdamNaj

This comment has been minimized.

Member

AdamNaj commented Oct 23, 2016

Implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment