在使用打字稿的HTML应用程序中,我有一个HTML表单。该表格有一些必填字段。在我的打字稿文件我建立一个错误信息是这样的:所需字段的较短验证功能
hasErrors() {
let errorCount = 0;
if (!item.Field1()) {
this.ErrorText('"Field1"');
errorCount++;
}
if (!item.Field2()) {
if (errorCount > 0)
this.ErrorText(this.ErrorText() + ', "Field2"');
else
this.ErrorText('"Field2"');
errorCount++;
}
if (!item.Field3()) {
if (errorCount > 0)
this.ErrorText(this.ErrorText() + ', "Field3"');
else
this.ErrorText('"Field3"');
errorCount++;
}
// ...
if (errorCount > 1)
this.ErrorText("The fields " + this.ErrorText() + " are required.";
else if (errorCount == 1)
this.ErrorText("The field " + this.ErrorText() + " is required.";
else
return false;
return true;
}
正如你可以看到,这个功能会非常长,如果有需要,许多领域。有没有更好/更短的方式来获得相同的结果?
谢谢,这就是我一直在寻找的。 –