0

I'm retrieving information for vehicle data from my API. And then validate the fields.

My ts file looks like this:

vehicleDetails = new CartellInfo();

ngOnInit() {
    this.claimForm = this.formBuilder.group({
        plateNumber: [ this.vehicleDetails.registration, [Validators.required] ],
        vehicleManufacturer: [ this.vehicleDetails.make, [Validators.required] ],
        vehicleModel: [ this.vehicleDetails.model, [Validators.required] ],
        vehicleRange: [ this.vehicleDetails.description, [Validators.required] ],
        chasisNumber: [ this.vehicleDetails.chassisNumber, [Validators.required] ],
        fuelType: [ this.vehicleDetails.fuelType, [Validators.required] ],
        dateFirstRegistered: [ this.vehicleDetails.firstRegistrationDate, [Validators.required] ],
        engineCapacity: [ this.vehicleDetails.engineCapacity, [Validators.required] ]
    });
}

findVehicle() {
    if (this.registration !== null) {

        this.cartellService.findVehicle(this.registration)
        .subscribe(
        res => {
            this.vehicleFound = true;
            this.vehicleDetails = res;
        });
    }
}

And the HTML for one of them looks like this:

<button class="btn btn-secondary" (click)="findVehicle()">FIND VEHICLE</button>

<input class="form-control"
    [ngClass]="{'mat-form-field-readonly': vehicleDetails.model || available }"
    [readonly]="vehicleDetails.model || available"
    name="vehicleModel"
    [formControl]="f.vehicleModel"
>
<mat-error *ngIf="submitted && f.vehicleModel.hasError('required')">
    Vehicle model is <strong>required</strong>
</mat-error>

But my input doesn't reflect data coming from the API. If you look code behind, the value is there, but it's not showing up.

What I'm doing wrong?

3
  • Here you are assigning kilometers: [ '', [Validators.required] ] empty values to kilometers, then how will you get the value in form field?? Commented Mar 5, 2020 at 15:44
  • I updated it to use another input... the kilometers one actually has another logic behind. The other one are not visible! So forget about the kilometers one. Gonna delete it. Commented Mar 5, 2020 at 15:54
  • Use tap to construct the FormGroup after the API call returns. Commented Mar 5, 2020 at 15:59

2 Answers 2

3

You need to set form after you receive the response.

this.cartellService.findVehicle(this.registration)
  .subscribe(
    res => {
      this.vehicleFound = true;
      this.vehicleDetails = res;
      this.claimForm = this.formBuilder.group({
          plateNumber: [ this.vehicleDetails.registration, [Validators.required] ],
          vehicleManufacturer: [ this.vehicleDetails.make, [Validators.required] ],
          vehicleModel: [ this.vehicleDetails.model, [Validators.required] ],
          vehicleRange: [ this.vehicleDetails.description, [Validators.required] ],
          chasisNumber: [ this.vehicleDetails.chassisNumber, [Validators.required] ],
          fuelType: [ this.vehicleDetails.fuelType, [Validators.required] ],
          dateFirstRegistered: [ this.vehicleDetails.firstRegistrationDate, [Validators.required] ],
          engineCapacity: [ this.vehicleDetails.engineCapacity, [Validators.required] ]
      });
    });

Or you can set the form value with the response.

this.cartellService.findVehicle(this.registration)
  .subscribe(
    res => {
      this.vehicleFound = true;
      this.vehicleDetails = res;
      this.claimForm.setValue({
          plateNumber: this.vehicleDetails.registration,
          vehicleManufacturer: this.vehicleDetails.make,
          vehicleModel: this.vehicleDetails.model,
          vehicleRange: this.vehicleDetails.description,
          chasisNumber: this.vehicleDetails.chassisNumber,
          fuelType: this.vehicleDetails.fuelType,
          dateFirstRegistered: this.vehicleDetails.firstRegistrationDate,
          engineCapacity: this.vehicleDetails.engineCapacity
      });
    });
Sign up to request clarification or add additional context in comments.

3 Comments

So that's more or less what I needed to do! I just updated the values for each form (instead of creating them, this is not happening on onInit, but on a button click). So just changing the values will work!
Just a suggestion, I believe template-driven form would be an good approach in your case as you can use the good old friend ngModel to bind values, if you prefer not to update manually.
@Shravan I will give it a try as it sounds like a better solution for me... Thanks for the suggestion!
0

In my case, it was necessary to run the change detection manually in the callback. This can be achieved by importing

constructor(private changeDetectorRef: ChangeDetectorRef) {
}

and running

this.changeDetectorRef.detectChanges();

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.