0

I have this model:

export class PlantArea {
  constructor(
    public id?: number,
    public code?: string,
    public name?: string,
    public plantId?: number,
    public plant?: Plant,
    public company?: Company
  ) {}
}

And I have set up this form:

form: FormGroup = new FormGroup({
    id: new FormControl(this.formData.id),
    code: new FormControl(this.formData.code, [Validators.required]),
    name: new FormControl(this.formData.name, [Validators.required]),
    company: new FormControl(this.formData.company, [Validators.required]),
    plant: new FormControl(this.formData.plant, [Validators.required]),
  });

I have a grid, where I have an edit button. Clicking on it shows the edit form, sets an input property, which binds the chosen entity to the form via this.form.reset(entity):

@Input() set model(entity: T) {
    console.log(entity);
    this.form.reset(entity);
    console.log(this.form.value);
    this.active = entity !== undefined;
    this.editMode = entity?.id ? true : false;
  }

The issue is, that by reseting the form values, the plant property gets dropped and the appr. control value isn't set. If I log the entity, the plant property is there, but if I log the form value, the plant property is gone:

enter image description here

Why is this? I can't figure out. There is a plant control on the form, so, it should bind the value to this, right?

derstauner
  • 1,478
  • 2
  • 23
  • 44
  • 1
    What is structure of `Plant`? This -> `plant: new FormControl(this.formData.plant, [Validators.required]),` should be FormGroup of `id` and `code`. If you set it as FormControl to set value it will expect like `plant:15` – navnath Oct 07 '21 at 08:18
  • I dont' think, it should be a FormGroup, because the same works for the `company` and this isn't a FormGroup. The `plant` is btw a simple object with `id` and `code` as you can see on the screenshot. The control on the form waits for an object value and not for a plain simly value. – derstauner Oct 07 '21 at 08:36
  • How do you edit the `plant` and `company` fields in the form? Can you share the code? It will be easy to help if you provide in on stackblitz – navnath Oct 07 '21 at 09:07

1 Answers1

0

It turned out, that was a false alarm. I have disabled the plant control initially and that's why the form.value hasn't the plant property.

See also this: disabled control do not ged included in the form value

derstauner
  • 1,478
  • 2
  • 23
  • 44