DropDownList Passing Validation Incorrectly

1 Answer 35 Views
DropDownList
Brian
Top achievements
Rank 2
Iron
Brian asked on 10 Nov 2023, 03:12 PM

Hi,

The following code snippet is passing validation incorrectly.  It is marked on the field and model as Required but is not being flagged as such when no option is selected.

HTML5 (form):

<div class="form-floating mb-3">
    @(Html.Kendo().DropDownListFor(m => m.PrimaryAnalystId).OptionLabel(new { Person = "[Select a person]", PersonId = 0 }).DataTextField("Person").DataValueField("PersonId").DataSource(s => s.Read(r => r.Action("GetPeople", "Utility", new { type = "analyst" }))).HtmlAttributes(new { @class = "form-select", required = "required" }))
    <label asp-for="PrimaryAnalystId">Primary Analyst</label>
</div>

C# (model):

[Required]
public int PrimaryAnalystId { get; set; }

TS/JS (validator):

const validator = $("#form1").kendoValidator({ validationSummary: { container: "#summary" } }).data("kendoValidator");

$(function () {
    $("#submit").on("click", function (event) {
        event.preventDefault();

        if (validator.validate()) {
            alert('valid');
        } else {
            alert('invalid');
        }
    });
});

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander
Telerik team
answered on 15 Nov 2023, 12:03 PM

Hi Brian,

Thank you for additionally taking the time to share the relevant code compartments that mold together the currently held scenario on your premises.

Upon further examination, it appears that the sporadical behavior would most notably persist from the fact that the .OptionLabel() API configuration utilizes its object overload.

As stated in the documentation:

"If the value is an object, then the widget will use it as a valid data item. Note that the optionLabel will not be available if the widget is empty."

With that in mind, a possible recommendation would be to pass a "string" value to the OptionLabel instead. This should then prohibit the form from being submitted based on your condition. For example:

@(Html.Kendo().DropDownListFor(m => m.PrimaryAnalystId).OptionLabel("[ Select a Person ]").DataTextField("Person").DataValueField("PersonId").ValuePrimitive(false).DataSource(s => s.Read(r => r.Action("GetPeople", "Home", new { type = "analyst" }))).HtmlAttributes(new { @class = "form-select", required = "required" }))

This is the behavior I managed to exhibit during some locally conducted experiments:

Please give this suggestion a try and let me know if this helps mitigate the validation issue at hand.

Kind Regards,
Alexander
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Brian
Top achievements
Rank 2
Iron
commented on 15 Nov 2023, 02:04 PM

That fixed it.  Thank you so much!
Tags
DropDownList
Asked by
Brian
Top achievements
Rank 2
Iron
Answers by
Alexander
Telerik team
Share this question
or