|
| 1 | +--- |
| 2 | +title: Dynamically Adjusting Form Field Validation Based on Available Options in the DropDownList Editor |
| 3 | +description: Learn how to make the Form fields required or non-required based on the available options of the DropDownList editors, which are configured for remote data binding. |
| 4 | +type: how-to |
| 5 | +page_title: Dynamically Adjusting Form Field Validation Based on Available Options in the DropDownList Editor |
| 6 | +slug: form-fields-validation-dropdownlist-data |
| 7 | +tags: form, fields, required, dropdownlist, remote, data, options, requestend |
| 8 | +res_type: kb |
| 9 | +ticketid: 1637672 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>{{ site.product }} Form</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Product</td> |
| 22 | +<td>{{ site.product }} DropDownList</td> |
| 23 | +</tr> |
| 24 | +<tr> |
| 25 | +<td>Product Version</td> |
| 26 | +<td>2024.3.806</td> |
| 27 | +</tr> |
| 28 | +</tbody> |
| 29 | +</table> |
| 30 | + |
| 31 | +## Description |
| 32 | + |
| 33 | +How can I dynamically set the fields in a Form as required or non-required based on the available options in their DropDownList editors? |
| 34 | + |
| 35 | +When using {{ site.product }} Form, which contains {{ site.product }} 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. |
| 36 | + |
| 37 | +## Solution |
| 38 | + |
| 39 | +1. Subscribe to the [`RequestEnd`](https://docs.telerik.com/{{ site.platform }}/api/kendo.mvc.ui.fluent/datasourceeventbuilder#requestendsystemstring) event of the DataSource of each DropDownList editor using a template delegate. |
| 40 | +1. 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. |
| 41 | +1. 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. |
| 42 | +1. Reuse the **validateField** function for all DropDownList editors that loads data from the server. |
| 43 | + |
| 44 | +```HtmlHelper |
| 45 | + @(Html.Kendo().Form<FormItems>() |
| 46 | + .Name("exampleForm") |
| 47 | + .HtmlAttributes(new { action = "Items", method = "POST" }) |
| 48 | + .Items(i => |
| 49 | + { |
| 50 | + i.Add() |
| 51 | + .Field(f => f.EquipmentType) |
| 52 | + .Label(l => l.Text("Equipment Type")) |
| 53 | + .Editor(e => |
| 54 | + { |
| 55 | + e.DropDownList() |
| 56 | + .DataTextField("ShortName") |
| 57 | + .DataValueField("EquipmentTypeID") |
| 58 | + .OptionLabel("Select") |
| 59 | + .DataSource(source => |
| 60 | + { |
| 61 | + source |
| 62 | + .Read(read => { read.Action("GetItems","Home"); }) |
| 63 | + .Events(ev => ev.RequestEnd(@<text> // Subscribe to the event by a template delegate. |
| 64 | + function(eventData) { // Access the event data. |
| 65 | + return validateField(eventData, "EquipmentType"); // Pass the event data and the name of the field to the custom function. |
| 66 | + } |
| 67 | + </text>)); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }) |
| 71 | + ) |
| 72 | +``` |
| 73 | +{% if site.core %} |
| 74 | +```TagHelper |
| 75 | + @addTagHelper *, Kendo.Mvc |
| 76 | + @model FormItems |
| 77 | +
|
| 78 | + <kendo-form name="exampleForm" form-data="@Model" method="POST" asp-action="Items"> |
| 79 | + <form-items> |
| 80 | + <form-item field="EquipmentType"> |
| 81 | + <item-label text="Equipment Type:" /> |
| 82 | + <dropdownlist-editor placeholder="Select" datatextfield="ShortName" datavaluefield="EquipmentTypeID"> |
| 83 | + <datasource on-request-end="function(eventData) { |
| 84 | + return validateField(eventData, 'EquipmentType'); |
| 85 | + }"> |
| 86 | + <transport> |
| 87 | + <read url="@Url.Action("GetItems", "Home")"/> |
| 88 | + </transport> |
| 89 | + </datasource> |
| 90 | + </dropdownlist-editor> |
| 91 | + </form-item> |
| 92 | + </form-items> |
| 93 | + </kendo-form> |
| 94 | +``` |
| 95 | +{% endif %} |
| 96 | +```Scripts |
| 97 | + <script> |
| 98 | + function validateField(eventData, fieldName) { // Reuse this handler for all editors that loads data from the server. |
| 99 | + if (eventData.response.length > 0) { |
| 100 | + $("#" + fieldName).attr("required", "required"); |
| 101 | + } else { |
| 102 | + $("#" + fieldName).removeAttr("required"); |
| 103 | + } |
| 104 | + } |
| 105 | + </script> |
| 106 | +``` |
| 107 | + |
| 108 | +## More {{ site.framework }} Form Resources |
| 109 | + |
| 110 | +* [{{ site.framework }} Form Documentation]({%slug htmlhelpers_form_aspnetcore_overview%}) |
| 111 | + |
| 112 | +* [{{ site.framework }} Form Demos](https://demos.telerik.com/{{ site.platform }}/form) |
| 113 | + |
| 114 | +{% if site.core %} |
| 115 | +* [{{ site.framework }} Form Product Page](https://www.telerik.com/aspnet-core-ui/form) |
| 116 | + |
| 117 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiforcore%}) |
| 118 | + |
| 119 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-core-ui) |
| 120 | + |
| 121 | +{% else %} |
| 122 | +* [{{ site.framework }} Form Product Page](https://www.telerik.com/aspnet-mvc/form) |
| 123 | + |
| 124 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiformvc%}) |
| 125 | + |
| 126 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-mvc) |
| 127 | +{% endif %} |
| 128 | + |
| 129 | +## See Also |
| 130 | + |
| 131 | +* [Server-Side API Reference of the Form for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/form) |
| 132 | +{% if site.core %} |
| 133 | +* [Server-Side TagHelper API Reference of the Form for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/taghelpers/form) |
| 134 | +{% endif %} |
| 135 | +* [Server-Side API Reference of the DropDownList for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/dropdownlist) |
| 136 | +{% if site.core %} |
| 137 | +* [Server-Side TagHelper API Reference of the DropDownList for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/taghelpers/dropdownlist) |
| 138 | +{% endif %} |
| 139 | +* [Telerik UI for {{ site.framework }} Breaking Changes]({%slug breakingchanges_2023%}) |
| 140 | +* [Telerik UI for {{ site.framework }} Knowledge Base](https://docs.telerik.com/{{ site.platform }}/knowledge-base) |
0 commit comments