2017-09-14 2 views
0

Ich benutze eckige 4 Form und Form-Group-Komponente, um ein hinzufügen/bearbeiten Datenformular zu erstellen. Ich bin in der Lage, korrekt zu validieren, wenn ich neue Daten hinzufüge, aber Probleme habe, wenn ich dasselbe Formular mit bereits gefüllten Eingabefeldern bearbeite.Wie validiere ich den Formularersteller während der Bearbeitung, wenn er vorgefüllte Werte enthält?

Below-Code ist der Teil Komponente:

this.saveNotification = this.formBuilder.group({ 
    id: '', 
    name: ['', Validators.required], 
    question: ['', Validators.required], 
    repeatTypesControl: ['', Validators.required], 
    remindAt: '', 
}); 

if(this.isUpdateCheck){///pre-filled form on Edit 
    this.saveNotification.patchValue({ 
    id: 4, 
    name: "Deck", 
    question: "Who's Deck is this?", 
    remindAt: "08:30pm", 
    }); 

this.saveNotification.updateValueAndValidity(); 
    for (var i in this.saveNotification.controls) { 
    this.saveNotification.controls[i].markAsTouched(); 
    } 

Below Code von Template-Teil ist:

<button ion-button color="secondary" type="submit" [disabled]="(!saveNotification.valid)" color="dark">SAVE</button> 

Ich bin immer saveNotification.valid Wert als falsch bekommen, weil bei der Bearbeitung die Werte werden immer pro von Komponente gefüllt. Ich habe dies versucht.saveNotification.controls [i] .markAsTouched(), aber das hat nicht funktioniert.

Antwort

0

INSED von

this.saveNotification.patchValue({ 
    id: 4, 
    name: "Deck", 
    question: "Who's Deck is this?", 
    remindAt: "08:30pm" 
}); 

Verwenden

this.saveNotification.controls['id'].setValue(4); 
this.saveNotification.controls['name'].setValue('Deck'); 
this.saveNotification.controls['question'].setValue('Who's Deck is this?'); 
this.saveNotification.controls['remindAt'].setValue('08:30pm'); 
+0

Dank aber über Code tun gleiche, was mein Code tut, ist es nicht machen Formular als gültig steuert. Was ich will, nachdem ich die Werte im Formular-Steuerelement gesetzt habe, dass "this.saveNotification.valid" wahr wird. – Abu

Verwandte Themen