# Conditional Form Validation

## Environment

<table>
    <tbody>
        <tr>
            <td>Product</td>
            <td>Form for Blazor</td>
        </tr>
    </tbody>
</table>

## 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](https://www.telerik.com/blazor-ui/documentation/components/form/overview) supports any [validator that is compatible with the Blazor EditForm and EditContext](https://www.telerik.com/blazor-ui/documentation/components/form/validation). There are several options to validate form values conditionally:

* Use a [third-party validator that allows conditional validation](https://www.telerik.com/blazor-ui/documentation/components/form/validation#fluent-validation).
* Perform custom validation in the [Form's `OnSubmit` event](https://www.telerik.com/blazor-ui/documentation/components/form/events#onsubmit).
* Implement [remote (server-side) custom validation](https://github.com/telerik/blazor-ui/tree/master/form/remote-validation).
* Use [`FormItem` Templates](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template). Subscribe to the **change** handlers of the field editors to execute custom logic, show notifications, etc.
* Implement a [custom conditional `DataAnnotations` attribute](https://www.telerik.com/blazor-ui/documentation/knowledge-base/validation-custom-dataannotations-validator). To see inline error messages next to the field editor, return the [`ValidationResult` overload that accepts the invalid field name(s)](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validationresult). In this way the form validator will know which field has failed validation.

  ```CS.skip-repl
  return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName),
      new List<string> { validationContext.MemberName });
  ```

> The Telerik components for Blazor do not perform the actual validation of the model. Validation is managed by the [`EditContext`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontext). The role of the Telerik components is to call `EditContext` methods, subscribe to `EditContext` events, retrieve validation messages, and display them. If a validation scenario does not work as expected, check the behavior in a standard Blazor `<EditForm>` to verify if the issue is related to the Telerik components.

## See Also

* [Custom `DataAnnotations` Validation](https://www.telerik.com/blazor-ui/documentation/knowledge-base/validation-custom-dataannotations-validator)
* [Fluent Validation](https://www.telerik.com/blazor-ui/documentation/components/form/validation#fluent-validation)
* [Live Demo: DateRangePicker Custom DataAnnotation Attribute](https://demos.telerik.com/blazor-ui/daterangepicker/validation)
