Bug/feature
I want to add a Validator to my checkbox so that I can only submit a form when the checkbox is checked
the form has a valid state whether or not the status of the checkbox is checked
Create a component and its template, and a reactive form group with a validator :
this.confirmForm = formBuilder.group({
reqCB: [false, Validators.required]
});
In the HTML, add a button that is disabled until the checkbox is checked :
One of the use-cases could be requiring a user to check the box if he has read the conditions of usage of the application
"@angular/animations": "^4.0.0",
"@angular/cli": "1.1.2",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/compiler-cli": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/flex-layout": "^2.0.0-beta.8",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.7",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"@types/parse": "^1.9.2",
Also you can use Validators.requiredTrue:
this.confirmForm = formBuilder.group({
reqCB: [false, Validators.requiredTrue]
});
Oh well, thank you @julianobrasil !
Is there a way of using that validator in a template driven form @julianobrasil ?
I don't think so. Angular docs docs says that just minlength, maxlength, and required work out-of-the-box with template driven forms. But you can take a look at here and try to build a custom one by your self.
Thanks @julianobrasil !
Most helpful comment
Also you can use
Validators.requiredTrue:this.confirmForm = formBuilder.group({ reqCB: [false, Validators.requiredTrue] });