.NET MAUI DataForm Validation
.NET MAUI DataForm provides built-in validation, which gives you full control over the data collected through the control.
The next sections list all DataForm members related to validation.
Validation modes
The selected mode is applied through ValidationMode
(of typeTelerik.Maui.Controls.DataFormValidationMode
) property of the DataForm control. You could choose between three validation modes:
-
Explicit
—The changes are validated explicitly by invoking theValidateCommand
or calling theValidateChanges
method of the DataForm. -
LostFocus
—The changes are validated after the editor loses focus. -
PropertyChanged
—The changes in the editor are validated immediately on each property change (when the property value changes).
When
ValidationMode
is set toLostFocus
, you have to setCommitMode
toLostFocus
orExplicit
.
The ValidationMode
can be applied globally to the RadDataForm
<telerik:RadDataForm x:Name="dataForm"
ValidationMode="LostFocus"/>
Validation properties
-
HasValidationErrors
(bool
)—Gets a value indicating whether there are validation errors.
Events
DataForm exposes the following events for validation:
-
ValidationCompleted
—Raised when the DataForm validation completes. TheValidationCompleted
event handler receives two parameters:-
sender
argument which is of type object, but can be cast to theRadDataForm
type. -
DataFormObjectValidationCompletedEventArgs
which probvides additional information for the validatedDataObject
, theValidationErros
(IReadOnlyList ofDataFormValidationError
) and whether there are validation errorsHasValidationErrors
(bool
).
-
-
EditorValidationCompleted
—Raised when the validation of an editor has completed. The EditorValidationCompleted event handler receives two parameters:-
sender
argument which is of type object, but can be cast to theRadDataForm
type. -
DataFormEditorValidationCompletedEventArgs
which probvides additional information for the validatedPropertyName
, the original value ff the validated propertyPropertyValue
(object
) in the model and the modified value of the validated property in the editor -EditorValue
(object
).
-
Manual Validation with Methods
DataForm exposes a ValidateChanges
method with two overloads:
-
ValidateChanges()
—Executes the validation logic associated with the DataForm control. This method is mostly useful when theValidationMode
is set toExplicit
. The method returnstrue
if the validation passes, otherwisefalse
.
this.dataForm.ValidateChanges();
-
ValidateChanges(string propertyName)
—Validates the pending changes in the editor for the specified property. This method is mostly useful when the DataFormValidationMode
property is set toExplicit
.True
if the validation passes,false
otherwise.
this.dataForm.ValidateChanges(propertyName);
Commands
-
ValidateCommand
(ICommand
)—Gets a command to execute the validation logic of the RadDataForm. This command is mostly useful when the DataFormValidationMode
property is set toExplicit
.
Validation summary - styling and customization
The ValidationSummaryMessage displays all validation messages for the editors. You can visualize/hide the validation summary message by setting the IsValidationSummaryVisible
(bool
) property to True
/False
.
You can use the following properties for validation styling and customization:
-
ValidationSummaryImageSource
(ImageSource
)—Specifies theImageSource
of the image displayed in the validation summary. -
ValidationSummaryImageStyle
(Style
)—Specifies the style applied to the image of the validationsummary. The target type of this style is the .NET MAUIImage
control. -
ValidationSummaryStyle
(Style
)—Specifies the style applied to the validation summary. The target type of this style is theTelerik.Maui.Controls.DataFormValidationSummaryView
.
-
ValidationSummaryLabelStyle
(Style
)—Specifies the style applied to the labels of the validation summary. The target type of this style is the .NET MAUILabel
control.
Error message - styling and customization
You can use the following properties for error message styling and customization:
-
ErrorImageSource
(ImageSource
)—Specifies theImageSource
of the image displayed in the error message. -
ErrorImageStyle
(Style
)—Specifies the style applied to the image of the error message. The target type of this style is the .NET MAUIImage
control. -
ErrorLabelStyle
(Style
)—Specifies the style applied to the labels of the error message. The target type of this style is the .NET MAUILabel
control.