-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slide-toggle): invalid required validator in template-driven forms (
#16547) Currently using the `slide-toggle` in a form using template-driven forms causes the slide-toggle to retrieve a wrong validator if the `required` attribute is set. This is because by default `@angular/forms` uses an input validator that ensures that the value is just defined. This is always the case for a slide-toggle since the value is always `true` or `false`. The solution to this problem is that we need to provide the checkbox validator for required slide-toggle components. The checkbox validator from the forms package ensures that the control is only valid if the slide-toggle is checked.
- Loading branch information
1 parent
ac09e37
commit dc0c271
Showing
8 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/material/slide-toggle/slide-toggle-required-validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { | ||
Directive, | ||
forwardRef, | ||
Provider, | ||
} from '@angular/core'; | ||
import { | ||
CheckboxRequiredValidator, | ||
NG_VALIDATORS, | ||
} from '@angular/forms'; | ||
|
||
export const MAT_SLIDE_TOGGLE_REQUIRED_VALIDATOR: Provider = { | ||
provide: NG_VALIDATORS, | ||
useExisting: forwardRef(() => MatSlideToggleRequiredValidator), | ||
multi: true | ||
}; | ||
|
||
/** | ||
* Validator for Material slide-toggle components with the required attribute in a | ||
* template-driven form. The default validator for required form controls asserts | ||
* that the control value is not undefined but that is not appropriate for a slide-toggle | ||
* where the value is always defined. | ||
* | ||
* Required slide-toggle form controls are valid when checked. | ||
*/ | ||
@Directive({ | ||
selector: `mat-slide-toggle[required][formControlName], | ||
mat-slide-toggle[required][formControl], mat-slide-toggle[required][ngModel]`, | ||
providers: [MAT_SLIDE_TOGGLE_REQUIRED_VALIDATOR], | ||
}) | ||
export class MatSlideToggleRequiredValidator extends CheckboxRequiredValidator {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters