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

6
  • 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) Commented Jun 24, 2016 at 1: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 Commented Jun 24, 2016 at 1: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 Commented Jun 24, 2016 at 1:56
  • I understand that, what is the best practice for this validation, javascript, "must be true", have a example ? Commented Jun 24, 2016 at 2:01
  • Start by looking at THE COMPLETE GUIDE TO VALIDATION IN ASP.NET MVC 3 - PART 2 to get you started. Also these answers Commented Jun 24, 2016 at 2:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.