New-UDForm w/ New-UDDynamic

Product: PowerShell Universal
Version: 3.7.10

I have a UDForm working 100% as-is, but wanted to try and make one subtle change–I don’t want the UDTextbox to disappear when submitted. I have added a New-UDElement and Set-UDElement and the UDTextBox sticks around (great!) but updating the text box and clicking Submit fails–nothing happens with the UDElement/UDDynamic data. The only fix is to reload the entire page (not great). Is there something I’m missing or some other way to accomplish the desired outcome? Ideally the UDTextbox would persist still be able to update the username and re-submit the form. Code:

New-UDPage -Name 'Test Dynamic Form' -Content {
    New-UDForm -Content {
        New-UDTextbox -Id 'ADUsername' -Placeholder 'Enter AD username'
    } -OnSubmit {
        $UserName = $EventData.ADUsername.Trim()

        If (-Not ([bool]([adsisearcher]"samaccountname=$UserName").FindOne()) ) {
            Show-UDToast "ERROR: Unable to find [$UserName]" -MessageColor Red -Position center -Duration 5000
        } Else {
            Set-UDElement -Id 'TestElement' -Content {
                New-UDDynamic -id "dyn_UserData" -Content {
                    $IconSuccess = New-UDIcon -Id "iconSuccess" -Icon CheckCircle -Size 2x -Color Green
                    $IconFailure = New-UDIcon -Id "iconFailure" -Icon Frown -Size 2x -Color Red
                    $Now = $([DateTime]::Now)
                    $UserProperties = 'Enabled','DistinguishedName','SamAccountname','LockedOut','AccountExpirationDate','PasswordExpired','memberof'
                    $UserData = Get-ADUser -Identity $UserName -Properties $UserProperties
                    New-UDList -SubHeader "Login Health for [$Username]. ($Now)" -Content {
                        Switch ($UserData.Enabled) {# Enabled Check
                            $false {
                                New-UDListItem -Label 'Account is disabled' -Icon $IconFailure
                                New-UDButton -Text "Enable $($UserData.SamAccountName)" -OnClick {
                                    Enable-ADAccount $UserData.SamAccountname
                                    sync-udelement -id "dyn_UserData"
                                }
                            } 
                            $true {
                                New-UDListItem -Label 'Account is enabled' -Icon $IconSuccess
                            }
                        }
                        Switch ($UserData.AccountExpirationDate) {# Account Expired Check
                            { $null -eq $_ } {
                                New-UDListItem -Label 'User account is not expired' -Icon $IconSuccess
                            }
                            { $_ -gt $Now } {
                                New-UDListItem -Label "User account expires [$($UserData.AccountExpirationDate)]" -Icon $IconSuccess
                            }
                            { $_ -lt $Now -and $null -ne $_ } {
                                New-UDListItem -Label "User account is expired. Expired [$($UserData.AccountExpirationDate)]" -Icon $IconFailure
                                New-UDDatePicker -Id 'dateExtensionDate' -Value (Get-Date -Format 'dd-MM-yyyy')
                                New-UDButton -Text "Extend $($UserData.SamAccountName)" -OnClick {
                                    $Date = (Get-UDElement -Id 'dateExtensionDate').Value
                                    Set-AdUser $UserData.SamAccountName -AccountExpirationDate $Date
                                } 
                            }
                        }
                        Switch -Regex ($UserData.DistinguishedName) { # DistinguishedName Check
                            { $_ -match 'OU=Bad' } {
                                New-UDListItem -Label 'User is located under "Bad" OU. Move object in AD and refresh' -Icon $IconFailure
                                Continue
                            }
                            { $_ -notmatch 'OU=Good' } {
                                New-UDListItem -Label "User account is located in invalid OU [$_]. Move object in AD and refresh" -Icon $IconFailure
                                Continue
                            }
                            { $_ -match "OU=Good" } {
                                New-UDListItem -Label 'User is in valid OU' -Icon $IconSuccess
                            }
                        }
                        Switch ($UserData.LockedOut) { # AD Lockout Check
                            $true {
                                New-UDListItem -Label 'Account is locked' -Icon $IconFailure
                                New-UDButton -Text "Unlock $Username" -OnClick {
                                    Unlock-ADAccount $UserData.SamAccountname
                                    sync-udelement -id "dyn_UserData"
                                }
                            }
                            $false {
                                New-UDListItem -Label 'Account is unlocked' -Icon $IconSuccess
                            }
                        }
                        switch ($UserData.PasswordExpired) {# Pasword Expired Check
                            $true {
                                New-UDListItem -Label 'Password has expired' -Icon $IconFailure -SubTitle 'User must change password'
                            } 
                            $false {
                                New-UDListItem -Label 'Password is not expired' -Icon $IconSuccess
                            }
                        }   
                    }
                    New-UDButton -Text 'Refresh User Data' -OnClick {
                        Sync-UDElement -id "dyn_UserData"
                    }
                } -LoadingComponent {
                    New-UDProgress -Circular
                }
            }
        } 
    }
    New-UDElement -Id 'TestElement' -Tag 'div' # Added so form doesn't disappear upon submittal
} -Url '/TestDynamicForm'

Hey @adam I’m curious if you may have insight on how to achieve the desired output (use UDForm with dynamic content that doesn’t cause UDTextbox to disappear).

Ended up messing with this and came up with something that appears to work consistently.

New-UDPage -Name 'User Login Health' -Content {
    New-UDAlert -Severity 'info' -Content {
        New-UDHtml "Reports the login health for specified AD user"
    }
    New-UDForm -Content {
        New-UDTextbox -Id 'ADUsername' -Placeholder 'Enter username to be searched'
    } -OnSubmit {
        $UserNameBeingChecked = $EventData.ADUsername.Trim()
        If (-Not ([bool]([adsisearcher]"samaccountname=$UserNameBeingChecked").FindOne()) ) {
            Show-UDToast "ERROR: Unable to find [$UserNameBeingChecked]. Check username and try again" -MessageColor Red -Position center -Duration 2500
        } Else {
            Set-UDElement -Id 'UserLoginHealthReport' -Content {
                New-UDDynamic -id "dyn_User" -Content {
                    [DateTime]$Now = [DateTime]::Now
                    $IconSuccess = New-UDIcon -Id "iconSuccess" -Icon CheckCircle -Size 2x -Color Green #Visual icon for healthy result
                    $IconFailure = New-UDIcon -Id "iconFailure" -Icon Frown -Size 2x -Color Red #Visual icon for unhealth result
                    $UserProperties = 'Enabled','DistinguishedName','SamAccountname','LockedOut','AccountExpirationDate','PasswordExpired','memberof'
                    $UserData = Get-ADUser -Identity $UserNameBeingChecked -Properties $UserProperties
                    New-UDList -SubHeader "Login Health for [$UserNameBeingChecked]. ($Now)" -Content {
                        # Enabled Check
                        Switch ($UserData.Enabled) {
                            $false {
                                New-UDListItem -Label 'Account is disabled' -Icon $IconFailure
                                New-UDButton -Text "Enable $($UserData.SamAccountName)" -OnClick {
                                    Enable-ADAccount $UserData.SamAccountname
                                    sync-udelement -id "dyn_User"
                                }
                            } 
                            $true {
                                New-UDListItem -Label 'Account is enabled' -Icon $IconSuccess
                            }
                        }
                        # Account Expired Check
                        Switch ($UserData.AccountExpirationDate) {
                            { $null -eq $_ } {
                                New-UDListItem -Label 'User account does not have an account expiration date' -Icon $IconSuccess
                            }
                            { $_ -gt $Now } {
                                New-UDListItem -Label "User account expires [$($UserData.AccountExpirationDate)]" -Icon $IconSuccess
                            }
                            { $_ -lt $Now -and $null -ne $_ } {
                                New-UDListItem -Label "User account is expired. Expired [$($UserData.AccountExpirationDate)]" -Icon $IconFailure
                                New-UDDatePicker -Id 'datepicker-NewExpirationDate' -Value (Get-Date)
                                New-UDButton -Text "Extend $($UserData.SamAccountName)" -OnClick {
                                    $NewAccountExpirationDate = (Get-Date (Get-UDElement -Id 'datepicker-NewExpirationDate').Value).Date
                                    If ($NewAccountExpirationDate -lt $Now) {
                                        Show-UDToast "ERROR: Provided date is in the past [$NewAccountExpirationDate]" -MessageColor Red -Position center -Duration 2500
                                    } Else {
                                        Set-AdUser $UserData.SamAccountName -AccountExpirationDate $NewAccountExpirationDate -ErrorAction Stop
                                        sync-udelement -id "dyn_User"
                                    }
                                } 
                            }
                        }
                        # AD Lockout Check
                        Switch ($UserData.LockedOut) {
                            $true {
                                New-UDListItem -Label 'Account is locked' -Icon $IconFailure
                                New-UDButton -Text "Unlock $UserNameBeingChecked" -OnClick {
                                    Unlock-ADAccount $UserData.SamAccountname
                                    Sync-UDElement -id "dyn_User"
                                }
                            }
                            $false {
                                New-UDListItem -Label 'Account is unlocked' -Icon $IconSuccess
                            }
                        }
                        # Pasword Expired Check
                        switch ($UserData.PasswordExpired) {
                            $true {
                                New-UDListItem -Label 'Password has expired' -Icon $IconFailure -SubTitle 'User must change password'
                            } 
                            $false {
                                New-UDListItem -Label 'Password is not expired' -Icon $IconSuccess
                            }
                        }   
                    }
                    New-UDButton -Text 'Refresh User Information' -OnClick {
                        sync-udelement -id "dyn_User"
                    }
                } -LoadingComponent {
                    New-UDProgress -Circular
                }
            }
        }
        Sync-UDElement -id "dyn_User"
    }
    New-UDElement -Id 'UserLoginHealthReport' -Tag 'div'
}