Fix required fields

This commit is contained in:
2023-03-22 00:05:08 +01:00
parent 17afc6fd2e
commit 606bb47203
2 changed files with 104 additions and 78 deletions

View File

@ -30,11 +30,11 @@ namespace Elwig.Helpers {
{ "423", Array.Empty<string[]>() },
};
public static ValidationResult CheckNumeric(TextBox input, bool optional) {
return CheckNumeric(input, optional, -1);
public static ValidationResult CheckNumeric(TextBox input, bool required) {
return CheckNumeric(input, required, -1);
}
private static ValidationResult CheckNumeric(TextBox input, bool optional, int maxLen) {
private static ValidationResult CheckNumeric(TextBox input, bool required, int maxLen) {
string text = "";
int pos = input.CaretIndex;
for (int i = 0; i < input.Text.Length; i++) {
@ -48,8 +48,7 @@ namespace Elwig.Helpers {
input.CaretIndex = pos;
if (text.Length == 0) {
if (optional) return new(true, null);
return new(false, "Wert ist nicht optional");
return required ? new(false, "Wert ist nicht optional") : new(true, null);
}
if (maxLen >= 0 && input.Text.Length > maxLen) {
@ -60,9 +59,9 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckPlz(TextBox input, bool optional, AppDbContext ctx) {
CheckNumeric(input, true, 4);
if (optional && input.Text.Length == 0) {
public static ValidationResult CheckPlz(TextBox input, bool required, AppDbContext ctx) {
CheckNumeric(input, false, 4);
if (!required && input.Text.Length == 0) {
return new(true, null);
} else if (input.Text.Length != 4) {
return new(false, "PLZ zu kurz");
@ -74,7 +73,7 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckPhoneNumber(TextBox input, bool optional) {
public static ValidationResult CheckPhoneNumber(TextBox input, bool required) {
string text = "";
int pos = input.CaretIndex;
for (int i = 0, v = 0; i < input.Text.Length && v < 30; i++) {
@ -112,15 +111,16 @@ namespace Elwig.Helpers {
input.Text = text;
input.CaretIndex = pos;
if (optional && text.Length == 0)
if (!required && text.Length == 0) {
return new(true, null);
if (text.Length < 10)
} else if (text.Length < 10) {
return new(false, "Telefonnummer zu kurz");
}
return new(true, null);
}
public static ValidationResult CheckEmailAddress(TextBox input, bool optional) {
public static ValidationResult CheckEmailAddress(TextBox input, bool required) {
string text = "";
int pos = input.CaretIndex;
bool domain = false;
@ -145,8 +145,7 @@ namespace Elwig.Helpers {
input.CaretIndex = pos;
if (text.Length == 0) {
if (optional) return new(true, null);
return new(false, "E-Mail-Adresse ist nicht optional");
return required ? new(false, "E-Mail_Adresse ist nicht optional") : new(true, null);
} else if (text[0] == '@' || !domain) {
return new(false, "E-Mail-Adresse ungültig");
}
@ -158,7 +157,7 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckIban(TextBox input, bool optional) {
public static ValidationResult CheckIban(TextBox input, bool required) {
string text = "";
int pos = input.CaretIndex;
int v = 0;
@ -184,7 +183,7 @@ namespace Elwig.Helpers {
input.Text = text;
input.CaretIndex = pos;
if (optional && text.Length == 0) {
if (!required && text.Length == 0) {
return new(true, null);
} else if (v < 5 || (text.StartsWith("AT") && v != 20) || (text.StartsWith("DE") && v != 22)) {
return new(false, "IBAN hat falsche Länge");
@ -199,7 +198,7 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckBic(TextBox input, bool optional) {
public static ValidationResult CheckBic(TextBox input, bool required) {
string text = "";
int pos = input.CaretIndex;
for (int i = 0, v = 0; i < input.Text.Length; i++) {
@ -217,8 +216,7 @@ namespace Elwig.Helpers {
}
if (text.Length == 0) {
if (optional) return new(true, null);
return new(false, "BIC ist nicht optional");
return required ? new(false, "BIC ist nicht optional") : new(true, null);
}
if (text.Length > 11) {
@ -238,11 +236,11 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckLfbisNr(TextBox input, bool optional) {
var res = CheckNumeric(input, true, 7);
public static ValidationResult CheckLfbisNr(TextBox input, bool required) {
var res = CheckNumeric(input, false, 7);
if (!res.IsValid) {
return res;
} else if (optional && input.Text.Length == 0) {
} else if (!required && input.Text.Length == 0) {
return new(true, null);
} else if (input.Text.Length != 7) {
return new(false, "Betriebsnummer zu kurz");
@ -253,16 +251,16 @@ namespace Elwig.Helpers {
return new(true, "Not implemented yet");
}
public static ValidationResult CheckUstId(TextBox input, bool optional) {
public static ValidationResult CheckUstId(TextBox input, bool required) {
// TODO
return new(true, "Not implemented yet");
}
public static ValidationResult CheckMgNr(TextBox input, bool optional, AppDbContext ctx, Member? m) {
var res = CheckNumeric(input, optional);
public static ValidationResult CheckMgNr(TextBox input, bool required, AppDbContext ctx, Member? m) {
var res = CheckNumeric(input, required);
if (!res.IsValid) {
return res;
} else if (optional && input.Text.Length == 0) {
} else if (!required && input.Text.Length == 0) {
return new(true, null);
}
@ -274,11 +272,11 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckPredecessorMgNr(TextBox input, bool optional, AppDbContext ctx) {
var res = CheckNumeric(input, optional);
public static ValidationResult CheckPredecessorMgNr(TextBox input, bool required, AppDbContext ctx) {
var res = CheckNumeric(input, required);
if (!res.IsValid) {
return res;
} else if (optional && input.Text.Length == 0) {
} else if (!required && input.Text.Length == 0) {
return new(true, null);
} else if (!Utils.MgNrExists(ctx, int.Parse(input.Text))) {
return new(false, "Ein Mitglied mit dieser Mitgliedsnummer existiert nicht");
@ -287,12 +285,12 @@ namespace Elwig.Helpers {
return new(true, null);
}
public static ValidationResult CheckDate(TextBox input, bool optional) {
public static ValidationResult CheckDate(TextBox input, bool required) {
// TODO
return new(true, "Not implemented yet");
}
public static ValidationResult CheckPartialDate(TextBox input, bool optional) {
public static ValidationResult CheckPartialDate(TextBox input, bool required) {
// TODO
return new(true, "Not implemented yet");
}