Skip to content

Checkboxes for non-nullable booleans fail validation when generated using @(Html.CheckBox() or @(Html.CheckBoxFor(m => m) #7940

@aleksandarevangelatov

Description

@aleksandarevangelatov

Bug report

Checkboxes for non-nullable booleans fail validation when generated using @(Html.CheckBox() or @(Html.CheckBoxFor(m => m)

Related to #7798

Reproduction of the problem

Model:

public bool Shipped { get; set; }

Implement an editable Grid and use a Boolean template for an editor where @(Html.CheckBoxFor(m => m) is used.

Current behavior

Upon saving the checkbox validation fails.

Expected/desired behavior

Validation shall succeed.

Workarounds

  1. Use a nullable bool if the checkbox should not be validated - public bool? Shipped { get; set; } or
  2. Use a Telerik UI Checkbox instead - @(Html.Kendo().CheckBoxFor(m => m) or
  3. Remove the generated data-val-required attribute when the checkbox is rendered - $('input[type="checkbox"]').removeAttr('data-val-required'); (use a more specific selector if needed) or
  4. Define a custom Boolean.cshtml editor and place it in ~Views\Shared\EditorTemplates\ folder where the visible <input type="checkbox" \> is wrapped in a container element and the data-val-required attribute is missing.
@model bool
@{
    var name = Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName("");
}

<div>
    <input id="@name" name="@name" type="checkbox" value="true" data-bind="checked:@name">
</div>
<input name="@name" type="hidden" value="true" data-bind="value:@name">
<span class="field-validation-valid" data-valmsg-for="@name" data-valmsg-replace="true"></span>

Environment

  • Kendo UI version: 202x.r.ddd
  • jQuery version: x.y
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions