Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactive Forms - Mat Select - doesn't trigger form or control as invalid after control has been touched, and validator.required is set to true #29092

Open
dreamstar-enterprises opened this issue May 17, 2024 · 7 comments

Comments

@dreamstar-enterprises
Copy link

dreamstar-enterprises commented May 17, 2024

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

Yes

Description

With this code

   this.contractEngagementInfo.addControl('jobTitle',
      this.formBuilder.control(null, [Validators.required]));
<!-- select -->
      <mat-select
        id="jobTitle"
        placeholder="Select a job title"
        formControlName="jobTitle"
        required>
        <mat-option>-- None --</mat-option>
        @for (jobTitle of allowableOptions;
          track jobTitle) {
          <mat-option [value]="jobTitle">{{jobTitle}}</mat-option>
        }
      </mat-select>

Current bug behaviour is that:

If the control gets loaded with data that is not amongst the allowable options, it will display

'Select a job title' - i.e. the placeholder.
If I touch the control and select None, the control (and form) will become invalid, saying the field is required - great!

However, if I touch the control and don't select None, or the other options, and unclick away from the contrl, I'd expect that since I have a validator.required validator on the cnotrl, that because I touched the control, it should ALSO say control (and form) are invalid, when I unclick away from it (just like what happens with a normal input field)

This currently doesn' happen, and means if the data loaded initially is wrong, a user can send back the same invalid data (since Angular never sees the form as invalid)

Screenshot 2024-05-17 at 19 28 15

Please provide a link to a minimal reproduction of the bug

Reproduction on Stackblitz

Please provide the exception or error you saw

See above

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

No response

@dreamstar-enterprises dreamstar-enterprises changed the title Reactive Forms - Mat Select - doesn't trigger form control invalid if loaded data is not amongst options Reactive Forms - Mat Select - doesn't trigger form or control as invalid if loaded data is not amongst permitted options May 17, 2024
@JeanMeche
Copy link
Member

Hi, can you please provide a working stackblitz repro, thank you.

@dreamstar-enterprises dreamstar-enterprises changed the title Reactive Forms - Mat Select - doesn't trigger form or control as invalid if loaded data is not amongst permitted options Reactive Forms - Mat Select - doesn't trigger form or control as invalid after control has been touched, and validator.required is set to true May 17, 2024
@dreamstar-enterprises
Copy link
Author

dreamstar-enterprises commented May 17, 2024

Pls see here:

https://stackblitz.com/edit/stackblitz-starters-gs9gmh?file=src%2Fmain.ts

(but, in Stackblitz, I get an allow any property add 'NO_ERRORS_SCHEMA' to the '@Component.schemas' of this component. [plugin angular-compiler] for the angular component)

@JoostK
Copy link
Member

JoostK commented May 17, 2024

The expected behavior here is debatable. From FormControl's perspective this is certainly working as expected, it doesn't have any knowledge of what "allowed" means; there is no validator present to verify that the value is among a set of expected values.

As for MatSelect, that is a bit tricky to define. Should it force-select the first option when it cannot match the value with an existing option, thereby updating the FormControl's value? That would cause your desired behavior, but it may not be everyone's desired behavior.

@dreamstar-enterprises
Copy link
Author

dreamstar-enterprises commented May 17, 2024

Fair points.
I think I can add a custom validator (to check if the value is in the allowable list), but how will this trigger, given that the value.required validator already doesn't trigger when I click / touch the control, when I initialise the component and form?

@dreamstar-enterprises
Copy link
Author

Hi, I still couldn't get this to work, in JetBrains.

@pkozlowski-opensource pkozlowski-opensource transferred this issue from angular/angular May 22, 2024
@pkozlowski-opensource
Copy link
Member

Could you please have a look at it from the MatSelect perspective? Feel free to open a form-related issue if there are limitations there.

@dreamstar-enterprises
Copy link
Author

dreamstar-enterprises commented May 23, 2024

I think I'll open another issue. Am I missing something though? Surely the behaviour should be the same as a normal input field - so the behaviour is consistent. Once it's touched, and if it is required, then after it has been touched, it should become invalid. Before it's touched, it's fine for it to be valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants