Ich arbeite an eckigen 4 Projekt. In dem verwendete ich reaktive Formen und Winkelvalidierungen. Alle Validierungsnachrichten werden im Fokus außerhalb des Felds angezeigt. Aber ich möchte auch Validierungsnachrichten auf der Schaltfläche "Senden" anzeigen. Html-Code:Wie man Validierungen auf submit oder blur für reaktive Formen von eckigen 4 anwenden?
<form [formGroup] = "addNewCustomerForm" (ngSubmit) = "submitAddNewCustomerForm()">
<div class="form-group">
<label>First Name</label>
<input class="form-control" [class.alert-border]="addNewCustomerForm.controls['first_name'].hasError('required') && addNewCustomerForm.controls['first_name'].touched" placeholder="Ex: James" formControlName="first_name">
</div>
<small *ngIf="addNewCustomerForm.controls['first_name'].hasError('required') && addNewCustomerForm.controls['first_name'].touched" class="required alert-error"><i class="material-icons">error</i> Please enter a valid first name.</small>
<small *ngIf="addNewCustomerForm.controls['first_name'].hasError('pattern') && addNewCustomerForm.controls['first_name'].touched && !addNewCustomerForm.controls['first_name'].hasError('required')" class="patern alert-error"><i class="material-icons">error</i> Please enter a valid first name.</small>
<div class="form-group">
<label>Last Name</label>
<input class="form-control" [class.alert-border]="addNewCustomerForm.controls['last_name'].hasError('required') && addNewCustomerForm.controls['last_name'].touched" placeholder="Ex: Lee" formControlName="last_name">
</div>
<small *ngIf="addNewCustomerForm.controls['last_name'].hasError('required') && addNewCustomerForm.controls['last_name'].touched" class="required alert-error"><i class="material-icons">error</i> Please enter a valid last name.</small>
<small *ngIf="addNewCustomerForm.controls['last_name'].hasError('pattern') && addNewCustomerForm.controls['last_name'].touched && !addNewCustomerForm.controls['last_name'].hasError('required')" class="patern alert-error"><i class="material-icons">error</i> Please enter a valid last name.</small>
<div class="form-group">
<label>Email Address</label>
<input class="form-control" [class.alert-border]="addNewCustomerForm.controls['email_id'].hasError('required') && addNewCustomerForm.controls['email_id'].touched" placeholder="Ex: [email protected]" formControlName="email_id" (focusin)="emailExist = false" (focusout)="checkEmailExistance($event)">
</div>
<small *ngIf="addNewCustomerForm.controls['email_id'].hasError('required') && addNewCustomerForm.controls['email_id'].touched" class="required alert-error"><i class="material-icons">error</i> Please enter a valid email.</small>
<small *ngIf="addNewCustomerForm.controls['email_id'].hasError('pattern') && addNewCustomerForm.controls['email_id'].touched && !addNewCustomerForm.controls['email_id'].hasError('required')" class="patern alert-error"><i class="material-icons">error</i> Please enter a valid email.</small>
<small *ngIf="emailExist" class="alert-error"><i class="material-icons">error</i> This email already exists. Please try with different email.</small>
</form>
ts Code:
this.addNewCustomerForm = this.formGroup.group({
first_name : [null, [Validators.required, Validators.pattern(this.regExpression)]],
last_name : [null, [Validators.required, Validators.pattern(this.regExpression)]],
email_id : [null, [Validators.required, Validators.pattern(this.emailPattern)]] });
submitAddNewCustomerForm(){
if(this.addNewCustomerForm.valid)
{
console.log(this.addNewCustomerForm.value);
}
}
wenn wir Ihren ersten Ansatz verwenden, wenn nur ein Feld Validierungsfehler hat, alle andere Felder auch gezeigt Validierungsnachricht, weil wir showError || verwenden Bedingung. –