Components: md-checkbox and required validator

Created on 6 Jul 2017  路  6Comments  路  Source: angular/components

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 :

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?

Most helpful comment

Also you can use Validators.requiredTrue:

this.confirmForm = formBuilder.group({ reqCB: [false, Validators.requiredTrue] });

All 6 comments

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 !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theunreal picture theunreal  路  3Comments

Hiblton picture Hiblton  路  3Comments

vitaly-t picture vitaly-t  路  3Comments

vanor89 picture vanor89  路  3Comments

constantinlucian picture constantinlucian  路  3Comments