Reactive For is always invalid when using dynamic patterns validation

Reactive Form having dynamic patten validation is always invalid on first load. Even after I edit it still invalid.

This is happening only when I am adding dynamic pattern matching validation.

Here is my use case :

  • On UI there is table and edit button in last row
  • Initially No formcontrols are visible
  • User Clicks on edit button then form control gets appear.

Refer this stackblitz

Below is code

<form [formGroup]="data_form">
    <table class="table table-border">
        <thead>

            <th>
                name
            </th>
            <th>
                age
            </th>
            <th><button class="btn btn-primary ">Save</button></th>
        </thead>
        <tbody>
            <ng-container *ngFor='let item of data;let j = index'>
                <tr>
                    <ng-container *ngFor="let td of keys;let i = index">
                        <ng-container>

                            <td  *ngIf="td !=='isEditable'">
                                {{item[td]}}
                                <input   [formControlName]="getControlName(j,i)" *ngIf="item.isEditable" type="text" name="" id="">
          </td>

                        </ng-container>
                    </ng-container>

                    <td>
                        <button (click)="item.isEditable = true"> Edit</button>
                    </td>
                </tr>
            </ng-container>
        </tbody>
    </table>

</form>

ts code :

import { Component } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular";
  data_form = new FormGroup({});
  public data;
  keys;

  ngOnInit() {
    this.data = [
      {
        name: "Sachin",
        age: 27,

        isEditable: false
      },
      {
        name: "Gopal",
        age: 27,

        isEditable: false
      },
      {
        name: "Pankaj",
        age: 24,

        isEditable: false
      }
    ];
    this.keys = Object.keys(this.data[0]);
    this.data.forEach((element, j) => {
      this.keys.forEach((k, i) => {
        this.data_form.addControl(
          "name_" + j + "_" + i,
          new FormControl(element[k], [Validators.required, Validators.pattern(/^[.\d]+$/)])
        );
      });
    });
  }

  log() {
    console.log(this.data);
  }

  getControlName(j, i) {
    return "name_" + j + "_" + i;
  }
}

Thanks in Advance.

EDIT:

Various Patterns that I have used :

  • Validators.pattern("^[a-zA-Z0-9 _/]+$")
  • Validators.pattern(/^[.\\d]+$/)
  • Validators.pattern(/^(yes|no)$/i)
  • Validators.pattern(/^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/)


Read more here: https://stackoverflow.com/questions/64403535/reactive-for-is-always-invalid-when-using-dynamic-patterns-validation

Content Attribution

This content was originally published by user1608841 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: