0

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?

0

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, but in this specific case I would need it to be possible, I had to find a way around, making my own validation and ignoring the form validation when I have the obj.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.