How to Validate FormArray length in angular2

后端 未结 6 1547
猫巷女王i
猫巷女王i 2021-02-02 09:16

I have a data driven form in angular2 like below

this.formBuilder.group({
  \'name\': [\'\',Validators.required],
  \'description\': [\'\', Validators.required],         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 10:04

    if you are trying to add validation to formarray try this may help you,

    this.formBuilder.group({
      'name': ['',Validators.required],
      'description': ['', Validators.required],
      'places': this.formBuilder.array(this.initPlaces()) 
    })
    
    initPlaces() {       
            return this._fb.group({
                places: ['',[Validators.minLength(1)]]           
            });
      }
    

    this initPlaces will simply return formGroup with validation as per requirement.

提交回复
热议问题