Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Validate FormArray length in angular2

I have a data driven form in angular2 like below

this.formBuilder.group({   'name': ['',Validators.required],   'description': ['', Validators.required],   'places': this.formBuilder.array([], Validators.minlength(1))  }) 

I want to add validations to the place formArray, so i am adding minlength validation, but minlength validation is not working on formArray.

What are the other validations for formArray, so that form must be valid only when places array contain atleast one place.

like image 626
chandradot99 Avatar asked Feb 12 '17 06:02

chandradot99


People also ask

How do I apply validation on FormArray?

Validating Angular FormArray First you need to add the required validators while creating a new product form group inside the addProduct method. Now let's add a span element adjacent to the input control. Add the following CSS to the app. component.

How do I set a FormArray value?

FormArray setValue() setValue() sets the value of the FormArray . We need to pass an array that matches the structure of the control. Create a sample FormArray . myFormArray = new FormArray([ new FormControl(), new FormControl(), new FormControl() ]);

How do I use FormArray in FormBuilder?

First, we need to import the FormArray from the Angular Forms Module. Build a formGroup orderForm using the FormBuilder. We define items as FormArray. We need to capture two fields under each item, the name of the item & description and price.


2 Answers

Validators.required does the magic for you:

// using required   this.formGroup = this.formBuilder.group({     taskTreeId: [Common.UID()],     executionProgrammedTime: ["", [Validators.required]],     description: [""],     tasks: this.formBuilder.array([], Validators.required) });    // using custom validation  export const minLengthArray = (min: number) => {   return (c: AbstractControl): {[key: string]: any} => {     if (c.value.length >= min)       return null;      return { MinLengthArray: true};   } }   this.formGroup = this.formBuilder.group({         taskTreeId: [Common.UID()],         executionProgrammedTime: ["", [Validators.required]],         description: [""],         tasks: this.formBuilder.array([], minLengthArray(2))     }); 
<button type="button" class="btn btn-success btn-rounded"      [disabled]="!formGroup.valid">SAVE</button> 
like image 145
Javier González Avatar answered Oct 05 '22 18:10

Javier González


Add this custom validator to your validation service:

minLengthArray(min: number) {     return (c: AbstractControl): {[key: string]: any} => {         if (c.value.length >= min)             return null;          return { 'minLengthArray': {valid: false }};     } } 

And then when creating the form do the following:

this.formBuilder.group({   'name': ['',Validators.required],   'description': ['', Validators.required],   'places': this.formBuilder.array([], this.validationService.minLengthArray(1))  }); 

And you can check errors against the FormArray by checking FormArray.hasError('minLengthArray')

like image 32
user1779362 Avatar answered Oct 05 '22 20:10

user1779362