Form validation always treats checkbox as required field

1 Answer 20 Views
Checkbox
Legion
Top achievements
Rank 1
Iron
Legion asked on 05 May 2024, 04:28 PM

I have a form on which there is checkbox control. For some reason the kendo validator always considers the checkbox a required field and considers a non-checked state as invalid. How can I get the validator to ignore the checkbox? As you can see, the field, IsTerminated is not marked required.



<form id="frmEmployee" method="post">
    <div class="control-grid">
        <div class="col-1-label">
            @Html.LabelFor(m => m.Id, "Employee Id: ")
        </div>
        <div class="col-1-control">
            @Html.Kendo().TextBoxFor(m => m.Id).HtmlAttributes(new { @readonly = "", @required = "", @validationMessage = "Id is required" })
        </div>

        <div class="col-2-label">
            @Html.LabelFor(m => m.FirstName, "First Name: ")
        </div>
        <div class="col-2-control">
            @Html.Kendo().TextBoxFor(m => m.FirstName).HtmlAttributes(new { @required = "", @validationMessage = "First Name is required" })
        </div>

        <div class="col-3-label">
            @Html.LabelFor(m => m.LastName, "Last Name: ")
        </div>
        <div class="col-3-control">
            @Html.Kendo().TextBoxFor(m => m.LastName).HtmlAttributes(new { @required = "", @validationMessage = "Last Name is required" })
        </div>

        <div class="col-1-label">
            @Html.LabelFor(m => m.MiddleName, "Middle Name: ")
        </div>
        <div class="col-1-control">
            @Html.Kendo().TextBoxFor(m => m.MiddleName)
        </div>

        <div class="col-2-label">
            @Html.LabelFor(m => m.PreferredName, "Preferred Name: ")
        </div>
        <div class="col-2-control">
            @Html.Kendo().TextBoxFor(m => m.PreferredName)
        </div>

        <div class="col-3-label">
            @Html.LabelFor(m => m.Email, "Email: ")
        </div>
        <div class="col-3-control">
            @Html.Kendo().TextBoxFor(m => m.Email).HtmlAttributes(new { @required = "", @validationMessage = "Email is required" })
        </div>

        <div class="col-1-label">
            @Html.LabelFor(m => m.HireDate, "Hire Date: ")
        </div>
        <div class="col-1-control">
            @Html.Kendo().DatePickerFor(m => m.HireDate).HtmlAttributes(new { @required = "", @validationMessage = "Hire Date is required" })
        </div>

        <div class="col-2-label">
            @Html.LabelFor(m => m.IsTerminated, "Terminated: ")
        </div>
        <div class="col-2-control">
            @Html.Kendo().CheckBoxFor(m => m.IsTerminated)
        </div>

        <div class="col-3-label">
            @Html.LabelFor(m => m.TerminationDate, "Termination Date: ")
        </div>
        <div class="col-3-control">
            @Html.Kendo().DatePickerFor(m => m.TerminationDate)
        </div>

        <div style="display: none;">
            @Html.Kendo().DatePickerFor(m => m.CreatedDate)
        </div>
    </div>
</form>

 

On page load I have:


    $(document).ready(function ()
    {
        $("#frmEmployee").kendoValidator();
    });

 

And before I submit my data to the server I have a check:


let validator = $('#frmEmployee').kendoValidator().data('kendoValidator');

if (validator.validate())
{
   // ...
}

validator.validate() always returns false if IsTerminated is not checked. IsTerminated is a non-nullable bool.

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 08 May 2024, 12:56 PM

Hello, Legion,

I tried to reproduce the behaviour in this Dojo example, but the validation passes if the Checkbox is not checked. Could you please try to modify the example to reproduce the issue?

Regards,
Martin
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Legion
Top achievements
Rank 1
Iron
commented on 08 May 2024, 05:49 PM

Does it matter that the model is an entity framework entity? ie. Would entity framework add some kind of Required annotation to an entity that could mess with the validation?
Martin
Telerik team
commented on 13 May 2024, 11:09 AM

Hello, Legion,

I cannot say if the framework would have something to do with it. You can inspect the Checkbox to see if a required attribute is assigned to the input element. 

Tags
Checkbox
Asked by
Legion
Top achievements
Rank 1
Iron
Answers by
Martin
Telerik team
Share this question
or