angular: A required checkbox should always be invalid if unchecked

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Given an <input type="checkbox"> with the required attribute, ngModel correctly considers it invalid to start with. Checking it correctly makes it valid, but after unchecking it it’s still valid.

Expected behavior

A required checkbox should always be invalid if it’s not checked.

Reproduction of the problem

http://plnkr.co/edit/GZI2fiZ65qtJQIjJbZLh?p=preview

What is the motivation / use case for changing the behavior?

HTML5 Form Validation doesn’t allow submitting a form if a required checkbox is unchecked. (Or at least that’s how Chrome and Firefox work.)

  • Angular version: 2.0.0-rc.6
  • Browser: [all]
  • Language: [all]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

you can you Validators.requiredTrue instead of Validators.required

Still observing the same issue in Angular version :7. Even with requiredTrue Validator, the experience is still the same. I tried writing a custom validator but the value once changed, remains the same. In my case I keep getting “on” as the value. It is killing me. Am I doing something wrong ?

For thoes using the reactive forms method and needing a quick hacked fix,

//part of the constructor 
    this.registerForm = fb.group({
        'answer': ['someAnswer', Validators.required],
         'confirmCheckbox':  ['', Validators.required]
     },
     {validator: checkCheckbox}) 
//the function
function checkCheckbox(c: AbstractControl){
    if(c.get('confirmCheckbox').value == false){
        return false;
    }else return true;
} 
// In the template
    <button type="submit" id="btnSubmitRegister" class="pwFeildButton" 
    [disabled]="!registerForm.valid || registerForm.get('confirmCheckbox').value != true">
       1337 buttonz
       </button>