Walidacja w Angularze

Walidacja w Angularze
Krzysztof Pe
  • Rejestracja:prawie 7 lat
  • Ostatnio:ponad 3 lata
  • Postów:78
0

Cześć,

Mam pewien problem z walidacją w Angularze 8. Podążam za samouczkiem z oficjalnej strony i ciągle dostaję "Cannot read property 'invalid' of undefined".

Kod HTML:

Kopiuj
<div class="container">
  <div class="row">
    <form [formGroup]="mathFactForm" (ngSubmit)="onSubmit(mathFactForm)">
      <label>
        Number:
        <input id="number" type="text" formControlName="number" required>
      </label>
      <div *ngIf="number.invalid && (number.dirty || number.touched)" class="alert alert-danger">

        <div *ngIf="number.errors.required">
          Name is required.
        </div>
      </div>
      <label>
        Fragment:
        <input type="text" formControlName="fragment">
      </label>
      <button type="submit" [disabled]="!mathFactForm.valid">Submit</button>
    </form>
  </div>
</div>

Kod TS:

Kopiuj
import { MathFactModel } from './../../models/MathFactModel';
import { FormBuilder, FormsModule, Validators, FormGroup, FormControl } from '@angular/forms';
import { NumbersApiService } from './../../services/numbers-api.service';
import { Component, OnInit, NgModule } from '@angular/core';

@Component({
  selector: 'app-math-facts',
  templateUrl: './math-facts.component.html',
  styleUrls: ['./math-facts.component.less']
})

export class MathFactsComponent implements OnInit {

  mathFactForm: FormGroup;
  numberControl: FormControl;
  fragmentControl: FormControl;

  mathFact;
  mathFactModel: MathFactModel;

  constructor(private numbersApiService: NumbersApiService,
    private formBuilder: FormBuilder) {
    this.mathFactForm = this.formBuilder.group({
      number: ['', Validators.required],
      fragment: ''
    });
  }

  ngOnInit() {
  }

  onSubmit(mathFactModel) {
    console.log(mathFactModel);
    console.log(this.mathFactForm.value);
  }
}

W momencie jak zmienię

Kopiuj
 <div *ngIf="number.invalid && (number.dirty || number.touched)" class="alert alert-danger">

na

Kopiuj
<div *ngIf="mathFactForm.invalid && (mathFactForm.dirty || mathFactForm.touched)" class="alert alert-danger">

Błąd przestaję się wyświetlać, ale to jest walidacja całego formularza, a nie potrafię się dobrać do walidacji poszczególnych kontrolek. Próbowałem się dobrać do konkretnych właściwości w ten sposób

Kopiuj
mathFactForm['controls']['number']

Niestety bez większego rezultatu. Ktoś wie gdzie co robię źle?

Pozdrawiam

A1
  • Rejestracja:ponad 8 lat
  • Ostatnio:prawie 3 lata
  • Postów:23
0
Kopiuj
 <div *ngIf="number.invalid && (number.dirty || number.touched)" class="alert alert-danger">

W tsie nigdzie nie masz czegos takiego jak number. Jesli chcesz sie w ten sposob odwloac do kontrolki to dodaj sobie jakiegos propertisa np.

Kopiuj
get number(){
return this.mathFactFrom.get('number');
}
Chramar
  • Rejestracja:około 7 lat
  • Ostatnio:ponad rok
  • Lokalizacja:Poznań
  • Postów:46
0

number jest słowem kluczowym (typem) Typescripta i nie polecam używania go jako nazwy zmiennej, czy czegokolwiek innego poza nazywaniem typu

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.