components icon indicating copy to clipboard operation
components copied to clipboard

md-checkbox and required validator

Open ghost opened this issue 9 years ago • 7 comments

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?

ghost avatar Jul 06 '17 12:07 ghost

Also you can use Validators.requiredTrue:

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

julianobrasil avatar Jul 06 '17 13:07 julianobrasil

Oh well, thank you @julianobrasil !

ghost avatar Jul 06 '17 13:07 ghost

Is there a way of using that validator in a template driven form @julianobrasil ?

jeanpchr avatar Jul 18 '17 18:07 jeanpchr

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.

julianobrasil avatar Jul 18 '17 19:07 julianobrasil

Thanks @julianobrasil !

jeanpchr avatar Jul 18 '17 19:07 jeanpchr

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.