Form
Form component for Universal Dashboard
Forms provide a way to collect data from users.
Forms can include any type of control you want. This allows you to customize the look and feel and use any input controls.
Data entered via the input controls will be sent back to the the OnSubmit script block when the form is submitted. Within the OnSubmit event handler, you will access to the $EventData variable that will contain properties for each of the fields in the form.
For example, if you have two fields, you will have two properties on $EventData.
1
New-UDForm -Content {
2
New-UDTextbox -Id 'txtTextField'
3
New-UDCheckbox -Id 'chkCheckbox'
4
} -OnSubmit {
5
Show-UDToast -Message $EventData.txtTextField
6
Show-UDToast -Message $EventData.chkCheckbox
7
}
Copied!

Supported Controls

The following input controls automatically integrate with a form. The values that are set within these controls will be sent during validation and in the OnSubmit event handler.

Simple Form

Simple forms can use inputs like text boxes and checkboxes.
1
New-UDForm -Content {
2
New-UDTextbox -Id 'txtTextfield'
3
New-UDCheckbox -Id 'chkCheckbox'
4
} -OnSubmit {
5
Show-UDToast -Message $EventData.txtTextfield
6
Show-UDToast -Message $EventData.chkCheckbox
7
}
Copied!

Formatting a Form

Since forms can use any component, you can use standard formatting components within the form.
1
New-UDForm -Content {
2
3
New-UDRow -Columns {
4
New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
5
New-UDTextbox -Id 'txtFirstName' -Label 'First Name'
6
}
7
New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
8
New-UDTextbox -Id 'txtLastName' -Label 'Last Name'
9
}
10
}
11
12
New-UDTextbox -Id 'txtAddress' -Label 'Address'
13
14
New-UDRow -Columns {
15
New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
16
New-UDTextbox -Id 'txtState' -Label 'State'
17
}
18
New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
19
New-UDTextbox -Id 'txtZipCode' -Label 'ZIP Code'
20
}
21
}
22
23
} -OnSubmit {
24
Show-UDToast -Message $EventData.txtFirstName
25
Show-UDToast -Message $EventData.txtLastName
26
}
Copied!

Returning Components

When a form is submitted, you can optionally return another component to replace the form on the page. You can return any Universal Dashboard component. All you need to do is ensure that the component is written to the pipeline within the OnSubmit event handler.
1
New-UDForm -Content {
2
New-UDTextbox -Id 'txtTextfield'
3
} -OnSubmit {
4
New-UDTypography -Text $EventData.txtTextfield
5
}
Copied!

Validating a Form

Form validation can be accomplished by using the OnValidate script block parameter
1
New-UDForm -Content {
2
New-UDTextbox -Id 'txtValidateForm'
3
} -OnValidate {
4
$FormContent = $EventData
5
6
if ($FormContent.txtValidateForm -eq $null -or $FormContent.txtValidateForm -eq '') {
7
New-UDFormValidationResult -ValidationError "txtValidateForm is required"
8
} else {
9
New-UDFormValidationResult -Valid
10
}
11
} -OnSubmit {
12
Show-UDToast -Message $Body
13
}
Copied!

Canceling a Form

You can define an -OnCancel event handler to invoke when the cancel button is pressed. This can be used to take actions like close a modal.
1
New-UDButton -Text 'On Form' -OnClick {
2
Show-UDModal -Content {
3
New-UDForm -Content {
4
New-UDTextbox -Label 'Hello'
5
} -OnSubmit {
6
Show-UDToast -Message 'Submitted!'
7
Hide-UDModal
8
} -OnCancel {
9
Hide-UDModal
10
}
11
}
12
}
Copied!

Displaying output without Replacing the form

Although you can return components directly from a form, you may want to retain the form so users can input data again. To do so, you can use Set-UDElement and a placeholder element that you can set the content to.
In this example, we have an empty form that, when submitted, will update the results element with a UDCard.
1
New-UDForm -Content {
2
3
} -OnSubmit {
4
Set-UDElement -Id 'results' -Content {
5
New-UDCard -Content { "Hello " + (Get-Date) }
6
}
7
}
8
9
New-UDElement -Id 'results' -Tag 'div'
Copied!

API

Last modified 1mo ago