New-UDForm auto-submit?

This one’s fairly niche, but I’ve got a dashboard page with a search form containing a single UDTextBox field in it to have a default value of an optional query string parameter in the URL, intended to allow an external application to automatically populate the user ID and submit.

It looks like this automatically submits when I tested it on a form that doesn’t have any validation in it, but my production form does validate and thus won’t submit automatically.

Is there a method to trigger the form submission that I could invoke conditional to that query string parameter being passed? Or will the user simply have to press the button themselves?

URL example: https://mydashboard.mycompany.com/dashboard/prod/dashpage?UserSearch=User123
Code excerpt:

New-UDForm -Content {
	New-UDTextbox -Id 'UsernameInput' -Value $UserSearch
	
} -OnValidate {

	if ($UserSearch) { 
		New-UDFormValidationResult -Valid 
	} 
	else {
		switch ($EventData.UsernameInput) {
			{ $null -eq $_ -or $_ -eq '' } {
				New-UDFormValidationResult -ValidationError "Please enter a user ID."
				break
			}
			{ $_ -notmatch '^[a-zA-Z0-9]+$' } { 
				New-UDFormValidationResult -ValidationError "Username must be alphanumeric characters only."
				break
			}
			{ $_.Length -lt 3 } { 
				New-UDFormValidationResult -ValidationError "Username must be at least 3 characters."
				break
			}
			Default {
				New-UDFormValidationResult -Valid
				break
			}
		}
	}
}

Cheers,

Adam B.

Product: PowerShell Universal
Version: 2.4.1

I don’t have a supe elegant solution but you might be able to use Invoke-UDJavaScript to simulate a click.

You’ll probably have to try to figure out the best way to find the submit button on the page.

Invoke-UAJavaScript "document.getElementById('btnSubmitButton').click()"

Feel free to open an issue for this. We could implement something like Invoke-UDForm.

1 Like

I tried using Invoke-UDJavaScript but it doesn’t seem to work, neither with that label or the longer one for the button I retrieved from Edge Dev tools.

Opened an issue on GitHub for a feature request.

Cheers

1 Like

Hey all, has anyone had any luck with this in 2.6.2? I am trying to do something similar from a dashboard that has a form for input. I have placed the Invoke-UDform cmdlt in every place I can think of (in the form, in the submit portion of the form, before the form definition, after the form definition but still on the dashboard page. Regardless of where I seem to place it, the Invoke does not seem to do anything. I threw a toast in there to ensure that I was hitting that conditional. This proved I was in the conditional. I appreciate any insight on this!

1 Like

quick update on this, i am able to invoke the form when triggered with a button, but have not worked out how to do this when the values for the form are passed in via the URL (have not been able to devote that much time to investigating though)

I just got this working the other day using a variable passed in the URL.

What I had to do to get it to auto-submit on load was add it at the top of the -OnValidate scriptblock in the form:

} -OnValidate {
    if ($URLVar) {
        Invoke-UDForm -Id 'UserSearchForm' -Validate
    }
    # rest of validation code
}

The page now loads and automatically submits the form as desired.

Hey @adamdavid85 oh awesome! I will give that a try with the validation scriptblock

Hey @adamdavid85 this worked wonderfully!! thanks for the insight on this. got some fine tuning, but the form now submits from URL.