Components: <mat-checkbox> class not updating to ng-invalid

Created on 13 Feb 2018  路  5Comments  路  Source: angular/components

Bug, feature request, or proposal:

The first time the component loads , the class is ng-invalid , if i check and uncheck the checkbox. The class does not update to ng-invalid.
I am using the reactive form.
I have set the Validators.required for this componenet

What is the expected behavior?

Class should update to ng-invalid

What is the current behavior?

Class is ng-valid , even though it should be ng-invalid

What are the steps to reproduce?

Providing a StackBlitz reproduction is the best way to share your issue.

StackBlitz starter: https://goo.gl/wwnhMV

StackBlitz - https://stackblitz.com/edit/angular-material2-issue-jlarze

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

"@angular/material": "^5.2.0"

Is there anything else we should know?

Most helpful comment

@gauravsoni1
See https://stackblitz.com/edit/angular-material2-issue-ftuvb7?file=app/app.component.ts

If you use Validators.required the form control will be valid when the checkbox value is false (the value when you uncheck the checkbox). This is because false is still a valid answer.

Use Validators.requireTrue to make form control invalid when checkbox is false.

All 5 comments

@gauravsoni1 Could you please provide a StackBlitz reproduction?

@tinayuangao - added Stackblitz to the issue.

@gauravsoni1
See https://stackblitz.com/edit/angular-material2-issue-ftuvb7?file=app/app.component.ts

If you use Validators.required the form control will be valid when the checkbox value is false (the value when you uncheck the checkbox). This is because false is still a valid answer.

Use Validators.requireTrue to make form control invalid when checkbox is false.

Validators.requireTrue works but it necessary to alter the class manually to render the checkbox as invalid in html. Should not checkbox behave in the same way as invalid input?

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._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theunreal picture theunreal  路  3Comments

constantinlucian picture constantinlucian  路  3Comments

jelbourn picture jelbourn  路  3Comments

LoganDupont picture LoganDupont  路  3Comments

alanpurple picture alanpurple  路  3Comments