2

I am stuck on how to add validators: I am using Reactive Forms and I am creating a array using the Form Builder ( an array of an class). Here is the how I am creating the form, and setting the control for the array:

Creating Form:

createForm(): void{
 this.workOrderForm = this.fb.group({
  workOrderStatus: ['', Validators.required],
  completedDate: '',
  workOrderNotes : this.fb.array([]);
 });
}

Then after the form is creating, I call a method to add another control which is an array of type WorkOrderNote:

setNotes(notes: WorkOrderNote[]) {
 const noteFGs = notes.map(note => this.fb.group(note));
 const notesFormArray = this.fb.array(noteFGs);
 this.workOrderForm.setControl('workOrderNotes', notesFormArray);
}

WorkOrderNote has property called notes. I'd like to set a required validator on it. I am unable to find how an example on the Angular tutorials..

1 Answer 1

2

You have to work some with the "map" of setNotes. e.g. If your "note" have properties: field1 and field2, you can use

   const noteFGs = notes.map(note => {
      return this.fb.group(
         {
          field1:[note.field1],
          field2:[note.field2,Validators.required],
         })
    });
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.