私はAnger 4プロジェクトに取り組んでいます。その中で、私は反応型と角度検証を使用しました。すべての検証メッセージは、フィールド外のフォーカスに表示されます。しかし、私は送信ボタンの上にも検証メッセージを表示したい。 HTMLコード:アングル4のリアクティブフォームのサブミットまたはブラーで検証を適用するにはどうすればよいですか?
<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コード:
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);
}
}
唯一のフィールドで検証エラーがある場合、我々は、あなたの最初のアプローチを使用している場合は、他のすべてのフィールドも表示検証メッセージ。なぜなら我々はshowError ||を使うからです。調子。 –