Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2844,6 +2844,11 @@ expect(panel.scrollTop)
subscription.unsubscribe();
}));

it('should set an asterisk after the label if the FormControl is required', fakeAsync(() => {
const label = fixture.nativeElement.querySelector('.mat-mdc-form-field label');
expect(label.classList).toContain('mdc-floating-label--required');
}));

});

describe('with custom error behavior', () => {
Expand Down Expand Up @@ -4539,7 +4544,8 @@ class InvalidSelectInForm {
template: `
<form [formGroup]="formGroup">
<mat-form-field>
<mat-select placeholder="Food" formControlName="food">
<mat-label>Food</mat-label>
<mat-select formControlName="food">
<mat-option *ngFor="let option of options" [value]="option.value">
{{option.viewValue}}
</mat-option>
Expand Down
4 changes: 4 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,10 @@ describe('MatSelect', () => {
subscription.unsubscribe();
}));

it('should set an asterisk after the label if the FormControl is required', fakeAsync(() => {
expect(fixture.nativeElement.querySelector('.mat-form-field-required-marker')).toBeTruthy();
}));

});

describe('with custom error behavior', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ import {
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import {ControlValueAccessor, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {
ControlValueAccessor,
FormGroupDirective,
NgControl,
NgForm,
Validators,
} from '@angular/forms';
import {
_countGroupLabelsBeforeOption,
_getOptionScrollPosition,
Expand Down Expand Up @@ -317,12 +323,14 @@ export abstract class _MatSelectBase<C> extends _MatSelectMixinBase implements A

/** Whether the component is required. */
@Input()
get required(): boolean { return this._required; }
get required(): boolean {
return this._required ?? this.ngControl?.control?.hasValidator(Validators.required) ?? false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also have a test for explicitly setting required overriding the form control required state?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The forms module will log a deprecation warning if required is used on an element with formControl.

}
set required(value: boolean) {
this._required = coerceBooleanProperty(value);
this.stateChanges.next();
}
private _required: boolean = false;
private _required: boolean | undefined;

/** Whether the user should be allowed to select multiple options. */
@Input()
Expand Down