0

I implemented validation "required" in my "checkbox 'and it works, the" set focus "is on the field, but the validation message is not displayed!

<form  asp-action="Create">
    <div class="form-horizontal">
        <h4>Etapa 4 - Termos de Uso</h4>
        <h5>Para concluir a sua inscrição basta validar que você não é um robo e concordar com os termos de uso!</h5>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    </div>
    <div class="form-group">
        <div class="g-recaptcha" data-sitekey="6LcgsyITAAAAAHiF8A1MGysKGUfQddq-_uzBD8ba"></div>
    </div>
    <div class="form-group">
        <div class="col-md-8  checkbox-inline">
            <input asp-for="Aceite" required  oninvalid="this.setCustomValidity('Para concluir a inscrição é necessário aceitar os termos de uso')"/>
            <a href="#" data-toggle="modal" data-target="#myModal">Li e concordo com os termos de uso e privacidade</a>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-8  checkbox-inline">
            <input asp-for="AceitePromocao" />
            <label asp-for="AceitePromocao"></label>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-pull-12">
            <input type="submit" value="Aceitar e concluir" class="btn btn-success btn-lg" />
        </div>
    </div>
</form>

enter image description here

Eriton Silva
  • 129
  • 1
  • 10
  • Which property are you expecting a validation error for? All you have shown is checkboxes for `bool` properties which will never have a validation error unless a malicious user submitted a `null` value (both `true` and `false` are valid for `bool`) –  Jun 24 '16 at 01:49
  • want to validate if it is selected, it seems to me that is validation is working, why not select when and try to send is not sent and the setfocus is appointed to the checkbox! but the message is not displayed – Eriton Silva Jun 24 '16 at 01:53
  • Because if its not checked it has a value of `false` which is a valid value for a `bool` property so therefore no error is shown. If you want to show an error message if its left unchecked, then you can just ad a `ModelState` error when you submit and return the view. Or you can write your own `[MustBeTrue]` validation attribute that implement `IClientValidatable` to get both client and server side validation –  Jun 24 '16 at 01:56
  • I understand that, what is the best practice for this validation, javascript, "must be true", have a example ? – Eriton Silva Jun 24 '16 at 02:01
  • Start by looking at [THE COMPLETE GUIDE TO VALIDATION IN ASP.NET MVC 3 - PART 2](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) to get you started. Also [these answers](http://stackoverflow.com/questions/6986928/enforcing-a-models-boolean-value-to-be-true-using-data-annotations) –  Jun 24 '16 at 02:04
  • In case anyone else comes here wondering where the "must be true" validator is. You can find the answer here. https://stackoverflow.com/a/64766089/5779818 – Joshua Poling Feb 13 '22 at 01:02

1 Answers1

0

This is because a checkbox required validation is not logical. A checkbox can always be true or false. It's true when you're checking it otherwise false.

That means it always have a value.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36