0

The angular material Stepper component can be connected with a form Group like this

<mat-vertical-stepper [linear]="isLinear" #stepper>
    <mat-step [stepControl]="firstFormGroup">
       <form [formGroup]="firstFormGroup">
          ....
      </form>
    </mat-step>
    ...

Stackblitz

Now, the interesting thing which I would like to understand is, if you leave the first step empty and click on the seconds step, the form field goes to an invalid state. But, as you can see in the stackblitz, at the bottom I list all states of the form and the form-field, but nothing seems to change.

So, my question is, which property is changed on the form by the Stepper to make the form invalid?

Note: In my case I need to add a message if this happens, something like this

<div *ngIf="firstFormGroup.touched && firstFormGroup.invalid">Extra message goes in here</div>
Armen Stepanyan
  • 1,618
  • 13
  • 29
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • The stepper component simply checks if the `AbstractControl` (an interface which `FormControl`, `FormGroup` and `FormArray` all inherit from) you passed to the `stepControl` input has been interacted with and is invalid, as shown here: https://github.com/angular/components/blob/ce7b92f96af75e44faed963e9a156eb763bf0806/src/cdk/stepper/stepper.ts#L199-L201 – Edric Feb 19 '21 at 10:57
  • It is not the stepper who renders the error messages, that is just mat-form-field and mat-error. So in order for those components to render the error, something should happen with the form – Jeanluca Scaljeri Feb 19 '21 at 11:03
  • No - any arbitrary input component (that supports `ControlValueAccessor` iirc) can be used for a reactive form. It's not related to the components but because the reactive form itself is invalid. – Edric Feb 19 '21 at 11:04
  • But why, if I click 'Next' go the input fields into the error state (show red border). Something should tell the form, show the errors, right? In [this](https://stackblitz.com/edit/angular-mmev7m-lusmlq?file=src%2Fapp%2Fstepper-vertical-example.html) stackblitz I have also disconnected the forms from the stepper, but still if I go to the next step, the form-fields go into the error state, super weird – Jeanluca Scaljeri Feb 20 '21 at 14:17

1 Answers1

0

Seems mat-stepper checks each form control validity. If you add fontrol errors, you will see what happens.

{{firstFormGroup.get('firstCtrl').errors | json}}<br />
{{secondFormGroup.get('secondCtrl').errors | json}}<br />

Now if you click next stepp and leave input empty, you will see this output

{ "required": true }
Armen Stepanyan
  • 1,618
  • 13
  • 29
  • Those errors are there from the beginning. I still don't see anything changing: [stackblitz](https://stackblitz.com/edit/angular-mmev7m?file=src%2Fapp%2Fstepper-vertical-example.html) – Jeanluca Scaljeri Feb 19 '21 at 10:33