0

I have inputs like this:

<mat-form-field *ngFor="let d of data">
    <mat-label>Item</mat-label>
    <input [(ngModel)]="d.item" matInput required>
</mat-form-field>`

I have tried this:

 <form #inputForm="ngForm">
        <mat-form-field *ngFor="let d of data">
            <mat-label>Item</mat-label>
            <input [name]="d.item" [(ngModel)]="d.item" matInput required>
        </mat-form-field>
    </form> 

<button class="confirmButton" [disabled]="!inputForm.valid" mat-button [mat-dialog-close]="data">Send</button>

but when I type something just in one of inputs button is enabled, any soultion to this?

0

2 Answers 2

0

You should use reactive forms for dynamically created forms.

Reactive forms provide a model-driven approach to handling form inputs whose values change over time. This guide shows you how to create and update a basic form control, progress to using multiple controls in a group, validate form values, and create dynamic forms where you can add or remove controls at run time.

See here

Sign up to request clarification or add additional context in comments.

Comments

0

In your html:

<form [formGroup]="testForm" (ngSubmit)="onSubmit()">
    <label>
    Required Field:
    <input type="text" formControlName="requiredField" required>
  </label>

    <button type="submit" [disabled]="!testForm.valid">Submit</button>
</form>

Then in your component:

  testForm = this.fb.group({
    requiredField: ['', Validators.required]
  });

  constructor(private fb: FormBuilder) { }

2 Comments

Because I still haven't read anything about reactive forms, can u tell me how should I populate that requiredField in loop?
This is a similar question to this one, I think. link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.