[英]SharePoint 2010/JavaScript Multiple Field Validations with Alerts
[英]How to throw multiple alerts in validations in Javascript
我正在嘗試驗證表單,每個表單值都有自己的驗證標准。 而且我需要拋出每個失敗的驗證消息作為警報。 我能夠這樣做,但我在同一條線上得到了這一點。 但是我需要將每個句子放在不同的行中。
我能夠在我的控制台中得到正確的答案(即不同行中的每個錯誤)。
我正在驗證每個驗證標准並將每個驗證的錯誤推送到數組中。 如果數組不為空,即如果我們遇到錯誤。 在將詳細信息提交給后端 API 時,它將返回該錯誤數組,否則返回 go。
if(errors.length != 0){
console.log("Not an empty array");
console.log(errors.join('\r\n'));
this.errorMsg = errors.join('\r\n');
}else{
this.service.createData(formData).subscribe((res) => {
console.log(res);
this.userForm.reset();
this.successMsg = res.message;
});
}
.html
<div *ngIf="errorMsg" class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>{{errorMsg}}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
正在發生的事情是將所有錯誤合並到一個errorMessage
中。 但是,您要做的是迭代並為每個錯誤顯示警報。 所以,它會是這樣的:
<div class="alert alert-danger alert-dismissible fade show" role="alert" *ngFor="let error of errors">
<strong>{{errorMsg}}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.