0

My HTML looks like this -

<form name="add_stepform" [formGroup]="ActionTypeForm" novalidate>
      <cdk-virtual-scroll-viewport [itemSize]="100" class="stepViewPort">
        <div [@fadeInOut] class="TestSteps example-item" 
             *cdkVirtualFor="let step of listOfSteps;TemplateCacheSize:0 let i=index">
          <div formArrayName="steps">
            <div [formGroupName]="i">
               <div class="form-group">
                  <label class="font16">Step Description</label>
                  <input id="stepDescription" type="text"
                    formControlName="stepDescription" class="form-control"
                    placeholder="ex: about the application" >
                    <mat-error *ngIf="stepDescription.errors?.required">
                        Description Required<br>
                    </mat-error>
                </div>
              </div>
           </div>
        </div>
     </cdk-virtual-scroll-viewport></form>

.ts file -

this.ActionTypeForm = new FormGroup({
  steps: new FormArray([this.testSteps()])})

testSteps() {
return new FormGroup({
  stepDescription: new FormControl('', [Validators.required)}
  get stepDescription(){
  return ((<FormArray>this.ActionTypeForm.get('steps')).controls)[0].get('stepDescription')
}

but its validating only first step description .. So any idea how to dynamically using index value ??

5
  • If possible then provide a stackblitz! Commented Nov 6, 2019 at 11:38
  • 1
    If you only has one error (required) you can remove the *ngIf and left the mat error like <mat-error>Description Required<br></mat-error> else you can use in the .html or pass as argument the "i" to a function step.get('stepDescription').errors?.required or ActionTypeForm.get('steps').at(i).get('stepDescription').errors?.required Commented Nov 6, 2019 at 11:43
  • hi @Eliseo Thanks for the solution .. it worked for me !! it was pretty simple :) Thanks a ton ! Commented Nov 6, 2019 at 11:53
  • @Eliseo do u have any idea how to validate input not to accept single quote or dobule quote as input ??? Commented Nov 6, 2019 at 11:57
  • I supouse you can achieve using the angular pattern validation -is using a 'regExp'- angular.io/api/forms/Validators#pattern Commented Nov 6, 2019 at 12:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.