dazzed
dazzed

Reputation: 669

Angular form invalid with valid controls

I'm creating a from group with existing values:

this.form = new FormGroup({
  field1: new FormControl(
    {
      value: obj ? obj.value : null,
      disabled: obj
    },
    Validators.required
  ),
  field2: new FormControl(
    {
      value: obj ? obj.value : null,
      disabled: obj
    },
    Validators.required
  )
});

reproduction: https://stackblitz.com/edit/angular-aadkss

I want it to when obj exists to fill the values, to be valid and disabled, but it is filling the values, being disabled and invalid, not valid. How can I make it valid?

Upvotes: 0

Views: 517

Answers (2)

Rbk
Rbk

Reputation: 72

can you put it into a stackblitz ? so that we can understand what exactly you mean ?

Upvotes: 0

Mike Jerred
Mike Jerred

Reputation: 10565

This is not possible, the value of .valid and .disabled depend upon the value of the control's .status which can only have one value, see https://github.com/angular/angular/blob/f27deea003822ed46437e74f102b281f692b7811/packages/forms/src/model.ts#L191

/**
  * The validation status of the control. There are four possible
  * validation status values:
  *
  * * **VALID**: This control has passed all validation checks.
  * * **INVALID**: This control has failed at least one validation check.
  * * **PENDING**: This control is in the midst of conducting a validation check.
  * * **DISABLED**: This control is exempt from validation checks.
  *
  * These status values are mutually exclusive, so a control cannot be
  * both valid AND invalid or invalid AND disabled.
  */
public readonly status!: string;

Upvotes: 1

Related Questions