md-checkbox and required validator
Bug, feature request, or proposal:
Bug/feature
What is the expected behavior?
I want to add a Validator to my checkbox so that I can only submit a form when the checkbox is checked
What is the current behavior?
the form has a valid state whether or not the status of the checkbox is checked
What are the steps to reproduce?
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 :
<button md-raised-button type="submit" [disabled]="confirmForm.invalid">Confirm
What is the use-case or motivation for changing an existing behavior?
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
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
"@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",
Is there anything else we should know?
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 !
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.