All checkboxes are being required regardless of [Required] attribute
I don't know what would have caused this, but we just noticed this during our development today. I noticed that the HTML output is adding the [data-rule-required] attribute, despite having no [Required] attribute on our model.
We only want our booleans to be required if the [Required] attribute is added to the view model. Amy I missing something?
We're using jQuery.Validation.Unobtrusive.Native.MVC5 (1.3.0) and jquery.validate.js (v1.15.0). Here's our code.
public class SomeViewModel
{
public bool Terms { get; set; }
}
@model SomeViewModel
@using (Html.BeginForm())
{
@Html.CheckBoxFor(m => m.Terms, true)
<button type="submit" class="btn btn-primary">Submit</button>
}
Am I missing something?
Shouldn't that be @Html.CheckBoxFor(m => m.Terms, true)?
Yes, my apologies. I accidentally removed that when copying it into GitHub. That's how I have it in my code.
I've edited my original post to reflect that.
To be clear, the error is still occurring. I had a typo in my original GitHub issue, but that typo is not in my codebase.
It's been a while since I've looked at this project. I'd advise debugging it. Should be pretty simple to do - lift the main project from this repo, put it in your project and replace the reference pointing to the nuget project with the local one.
And looking at the unit tests this appears to be intentional behaviour: https://github.com/johnnyreilly/jQuery.Validation.Unobtrusive.Native/blob/master/jVUNUnitTests/CheckBoxExtensionsRequiredTests.cs
What does @Html.CheckBoxFor(m => m.Terms) generate by the way? The behaviour should be the same even if the generated attributes differ