Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 2.54 KB

form-conditional-validation.md

File metadata and controls

58 lines (42 loc) · 2.54 KB
title description type page_title slug position tags ticketid res_type category
Conditional Form Validation
How to implement dynamic custom conditional form validation with the Telerik Blazor Form.
how-to
How to Use Conditional Form Validation
form-kb-conditional-validation
form, validation
1543336, 1558247, 1560189
kb
knowledge-base

Environment

Product Form for Blazor

Description

How to apply custom conditional validation, based on user selection?

How to implement conditional required validation for a form field? There are fields that I want to set as required only when another field has a certain value.

How to set dynamic validation for Blazor form fields? I want to update the validation criteria of a TextBox at runtime.

Solution

The TelerikForm supports any validator that is compatible with the Blazor EditForm and EditContext. There are several options to validate form values conditionally:

  • Use a third-party validator that allows conditional validation.

  • Perform custom validation in the Form's OnSubmit event.

  • Implement remote (server-side) custom validation.

  • Use FormItem Templates. Subscribe to the change handlers of the field editors to execute custom logic, show notifications, etc.

  • Implement a custom conditional DataAnnotations attribute. To see inline error messages next to the field editor, return the ValidationResult overload that accepts the invalid field name(s). In this way the form validator will know which field has failed validation.

    return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName),
        new List<string> { validationContext.MemberName });

@template

See Also