# Dynamically Adjusting Form Field Validation Based on Available Options in the DropDownList Editor

## Environment

<table>
<tbody>
<tr>
<td>Product</td>
<td>Telerik UI for ASP.NET MVC Form</td>
</tr>
<tr>
<td>Product</td>
<td>Telerik UI for ASP.NET MVC DropDownList</td>
</tr>
<tr>
<td>Product Version</td>
<td>2024.3.806</td>
</tr>
</tbody>
</table>

## Description

How can I dynamically set the fields in a Form as required or non-required based on the available options in their DropDownList editors?

When using Telerik UI for ASP.NET MVC Form, which contains Telerik UI for ASP.NET MVC DropDownList editors that bind to remote data, you can make the fields non-required in cases when no options are received from the server. Otherwise, the users will be forced to select an option to submit the Form.

## Solution

1. Subscribe to the [`RequestEnd`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/datasourceeventbuilder#requestendsystemstring) event of the DataSource of each DropDownList editor using a template delegate.
2. Within the inline event handler, access the event data and pass it to a JavaScript function along with the name of the field to which the DropDownList binds.
3. Within the JavaScript function **validateField**, check if there are available options received from the server and set the attribute `required` to the input element of the respective field with jQuery.
4. Reuse the **validateField** function for all DropDownList editors that loads data from the server.

```Razor HtmlHelper
    @(Html.Kendo().Form<FormItems>()
        .Name("exampleForm")
        .HtmlAttributes(new { action = "Items", method = "POST" })
        .Items(i =>
        {
            i.Add()
            .Field(f => f.EquipmentType)
            .Label(l => l.Text("Equipment Type"))
            .Editor(e =>
            {
                e.DropDownList()
                .DataTextField("ShortName")
                .DataValueField("EquipmentTypeID")
                .OptionLabel("Select")
                .DataSource(source =>
                {
                    source
                        .Read(read => { read.Action("GetItems","Home"); })
                        .Events(ev => ev.RequestEnd(@<text> // Subscribe to the event by a template delegate.
                            function(eventData) { // Access the event data.
                                return validateField(eventData, "EquipmentType"); // Pass the event data and the name of the field to the custom function.
                            }
                        </text>));
                });
            });
        })
    )
```

```JS scripts
    <script>
        function validateField(eventData, fieldName) { // Reuse this handler for all editors that loads data from the server.
            if (eventData.response.length > 0) {
                $("#" + fieldName).attr("required", "required");
            } else {
                $("#" + fieldName).removeAttr("required");
            }
        }
    </script>
```

## More ASP.NET MVC Form Resources

* [ASP.NET MVC Form Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/layout/form/overview)

* [ASP.NET MVC Form Demos](https://demos.telerik.com/aspnet-mvc/form)

* [ASP.NET MVC Form Product Page](https://www.telerik.com/aspnet-mvc/form)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Server-Side API Reference of the Form for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/form)

* [Server-Side API Reference of the DropDownList for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/dropdownlist)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
