1

I have a problem updating the values of car_description. Initially, the car_description is set to required. But if value.car_description isn't null then I need to patch its value from the onSelectPlateNo(). and it should be disabled so you can't change it.

My problem now is that I can't submit it, since it is still required even though the values are already set.

this.serviceTransactionForm = this.formBuilder.group({
    car_description: [null, Validators.required],
});



onSelectPlateNo(value) {
    if (value.car_description) {
      this.serviceTransactionForm.get('car_description').clearValidators();
      this.serviceTransactionForm.get('car_description').setValidators(null);
      this.serviceTransactionForm.get('car_description').updateValueAndValidity();
      this.serviceTransactionForm.get('car_description').disable();
      this.serviceTransactionForm.get('car_description').setValue(value.car_description);
    } else {
      this.serviceTransactionForm.get('car_description').enable();
    }
  }
0

2 Answers 2

1

A formControl can either be

  • VALID
  • INVALID
  • DISABLED
  • PENDING

By

this.serviceTransactionForm.get('car_description').disable();

you're setting the status of this form field to DISABLED. The values of

this.serviceTransactionForm.get('car_description').valid

and

this.serviceTransactionForm.get('car_description').invalid

are both false.

Two possible solutions are:

  1. Negate the check: Instead of

    if (this.serviceTransactionForm.valid)
    

    check for

    if (!this.serviceTransactionForm.invalid)
    
  2. Use readOnly instead of disabled in the HTML template

    <input type="text" formControlName="car_description" [readonly]="car_description_read_only">
    

You can reduce your code to

this.serviceTransactionForm = this.formBuilder.group({
    car_description: [null, Validators.required],
});

car_description_read_only = false;

onSelectPlateNo(value) {
    if (value.car_description) {
      this.serviceTransactionForm.get('car_description').setValue(value.car_description);
      this.car_description_read_only = true;
    } else {
      this.serviceTransactionForm.get('car_description').enable();
    }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

I'm thinking of readOnly too but how can you set it to that?
@Joseph I added an example for readonly. You need a variable for this
0

You can use removeControl. Example in your case: this.serviceTransactionForm.removeControl('car_description')

1 Comment

This causes that the user can change the value and get error messages in the log: stackblitz.com/edit/angular-ivy-ddvfsn?file=src/app/… Also OP wants to submit the whole form.

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.