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
Class should update to ng-invalid
Class is ng-valid , even though it should be ng-invalid
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
"@angular/material": "^5.2.0"
@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._
Most helpful comment
@gauravsoni1
See https://stackblitz.com/edit/angular-material2-issue-ftuvb7?file=app/app.component.ts
If you use
Validators.requiredthe form control will be valid when the checkbox value isfalse(the value when you uncheck the checkbox). This is becausefalseis still a valid answer.Use
Validators.requireTrueto make form control invalid when checkbox is false.