0

I make a simple form with angular using a reactive form but I have an error, i don't understand why it tells me that it lacks :

I've already had forms created many times but its the first time that I have this error.

enter image description here

ts.file

  productForm: any = FormGroup;

  constructor(private fb: FormBuilder,
    public dialogRef: MatDialogRef<DialogFormComponent>, 
    @Inject(MAT_DIALOG_DATA) public data:any) { }

  ngOnInit() {
    this.initDialogForm();
  }

  initDialogForm() {
    this.productForm = this.fb.group({
      id: [this.data?.id],
      name: [this.data?.name , Validators.required],
      price: [this.data?.price , Validators.required],
      comment: [this.data?.comment , Validators.required],
      date : [this.data?.date]
    });
  }

  onSubmit() {
    this.dialogRef.close(this.productForm.value);
  }

}

html

mat-dialog-content>
    <form [formGroup]="productForm" (ngSubmit)="onSubmit()">
        <div class="from-group">
            <label for="name">Nom</label>
            <input id="name" type="text" formControlName="name" class="form-control">
        </div>
        <div class="from-group">
            <label for="price">Prix</label>
            <input id="price" type="number" formControlName="price" class="form-control">
        </div>
        <div class="from-group">
            <label for="comment">Commentaire</label>
            <input id="comment" type="text" formControlName="comment" class="form-control">
        </div>
        <div class="from-group">
            <label for="expiration">date de péremption</label>
            <input id="expiration" type="date" formControlName="date" class="form-control">
        </div>
        <mat-dialog-actions>
            <button>Valider</button>
        </mat-dialog-actions>
    </form>
</mat-dialog-content>

1 Answer 1

1

? means conditional operataor inside formgroup thats whys compilar think that you have missing condition after :

   this.productForm = this.fb.group({
      id: [this.data.id],
      name: [this.data.name , [Validators.required]],
      price: [this.data.price , [Validators.required]],
      comment: [this.data.comment , [Validators.required]],
      date : [this.data.date]
    });
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.