jQuery.Validation.Unobtrusive.Native icon indicating copy to clipboard operation
jQuery.Validation.Unobtrusive.Native copied to clipboard

All checkboxes are being required regardless of [Required] attribute

Open travislaynewilson opened this issue 9 years ago • 5 comments

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?

travislaynewilson avatar Aug 26 '16 18:08 travislaynewilson

Shouldn't that be @Html.CheckBoxFor(m => m.Terms, true)?

johnnyreilly avatar Aug 26 '16 18:08 johnnyreilly

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.

travislaynewilson avatar Aug 26 '16 23:08 travislaynewilson

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.

travislaynewilson avatar Aug 30 '16 16:08 travislaynewilson

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.

johnnyreilly avatar Aug 30 '16 17:08 johnnyreilly

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

johnnyreilly avatar Aug 30 '16 17:08 johnnyreilly