3

How do I set validators on a nested FormControl with child objects? This product below, actually has members: productId, productName, productDescription (coming from a dropdown select) I want to set validators productId > 4 .

this.editSharedForm = this.formBuilder.group({
   'customerName':[null,[Validators.maxLength(50)],
   'customerPhone': [null, [Validators.maxLength(10)]],
   'product': new FormControl('')

Following is not working:

this.editSharedForm.get('product').get('productId').setValidators(Validators.min(5))

this.editSharedForm.get('product').value['productId'].setValidators(Validators.min(5))

Errors being Undefined error for first one, not a function for second command.

5
  • your control name is product where is productId? Commented Jan 18, 2020 at 4:38
  • Post the all (child formGroup) structure also! Commented Jan 18, 2020 at 4:39
  • Simply use this.editSharedForm.get('product').setValidators(yourValidators) and then call this.editSharedForm.get('product').updateValueAndValidity() in order to run the new validators. Commented Jan 18, 2020 at 22:55
  • 'product':this.formBuilder.group({productId:new FormControl(null)}) Commented Jan 20, 2020 at 11:12
  • 1
    hi @AndreiGătej feel free to place in answer, and I can send points, thanks Commented Jan 29, 2020 at 17:49

1 Answer 1

1

You can simply use

this.editSharedForm.get('product').setValidators(yourValidators)

and then call

this.editSharedForm.get('product').updateValueAndValidity()

in order to run the new validators.

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

Comments

Your Answer

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