typescriptを使用したHTMLアプリケーションでは、私はHTMLフォームを持っています。このフォームにはいくつかの必須フィールドがあります。私のtypescriptファイルでは、私はこのようなエラーメッセージ構築しています:必要とされる多くのフィールドがある場合、あなたが見ることができるように必要なフィールドの検証機能の短縮
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;
}
を、この機能は非常に長くなります。同じ結果を得るためのより良い/より短い方法がありますか?
ありがとう、それは私が探していたものです。 –