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");
}

View File

@ -18,6 +18,7 @@ namespace Elwig.Windows {
private bool IsCreating = false;
private List<string> TextFilter = new();
private readonly Control[] ExemptInputs;
private readonly Control[] RequiredInputs;
private IEnumerable<TextBox> TextBoxInputs = Array.Empty<TextBox>();
private IEnumerable<ComboBox> ComboBoxInputs = Array.Empty<ComboBox>();
private IEnumerable<CheckBox> CheckBoxInputs = Array.Empty<CheckBox>();
@ -36,6 +37,11 @@ namespace Elwig.Windows {
NewMemberButton, EditMemberButton, DeleteMemberButton,
ResetButton, SaveButton, CancelButton
};
RequiredInputs = new Control[] {
MgNrInput, GivenNameInput, FamilyNameInput,
AddressInput, PlzInput,
BusinessSharesInput, BranchInput, DefaultKgInput
};
}
private void Window_Loaded(object sender, RoutedEventArgs evt) {
@ -55,6 +61,10 @@ namespace Elwig.Windows {
base.OnClosing(evt);
}
private bool SenderIsRequired(object sender) {
return (sender is Control c) && RequiredInputs.Contains(c);
}
private async Task RefreshMemberList() {
await Context.Members.LoadAsync();
await RefreshMemberListQuery();
@ -127,6 +137,14 @@ namespace Elwig.Windows {
ActiveInput.IsChecked = true;
ContactPostInput.IsChecked = true;
FillOriginalValues();
foreach (var input in RequiredInputs) {
if (input is TextBox tb && tb.Text.Length == 0) {
Utils.SetInputInvalid(input);
Valid[input] = false;
} else if (input is ComboBox cb && cb.SelectedItem == null) {
Utils.SetInputInvalid(input);
}
}
}
private void MemberList_SelectionChanged(object sender, RoutedEventArgs evt) {
@ -178,8 +196,7 @@ namespace Elwig.Windows {
}
private async void SaveButton_Click(object sender, RoutedEventArgs evt) {
Member m = IsEditing ? (Member)MemberList.SelectedItem : Context.CreateProxy<Member>();
await UpdateMember(m);
Member m = await UpdateMember(IsEditing ? (Member)MemberList.SelectedItem : Context.CreateProxy<Member>());
IsEditing = false;
IsCreating = false;
MemberList.IsEnabled = true;
@ -196,6 +213,7 @@ namespace Elwig.Windows {
if (IsEditing) {
RefreshInputs();
} else if (IsCreating) {
ClearInputs();
InitInputs();
}
UpdateButtons();
@ -302,7 +320,7 @@ namespace Elwig.Windows {
ActiveMemberInput.IsEnabled = true;
}
private async Task UpdateMember(Member m) {
private async Task<Member> UpdateMember(Member m) {
int newMgNr = int.Parse(MgNrInput.Text);
m.PredecessorMgNr = (PredecessorMgNrInput.Text == "") ? null : int.Parse(PredecessorMgNrInput.Text);
m.Prefix = (PrefixInput.Text == "") ? null : PrefixInput.Text;
@ -354,6 +372,8 @@ namespace Elwig.Windows {
if (newMgNr != m.MgNr) {
await Context.Database.ExecuteSqlAsync($"UPDATE member SET mgnr = {newMgNr} WHERE mgnr = {m.MgNr}");
await Context.Members.LoadAsync();
m = await Context.Members.FindAsync(newMgNr);
}
} catch (Exception exc) {
if (tr != null) await tr.ReloadAsync();
@ -361,6 +381,8 @@ namespace Elwig.Windows {
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
MessageBox.Show(str, "Mitglied aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error);
}
return m;
}
private void FillInputs(Member m) {
@ -502,7 +524,7 @@ namespace Elwig.Windows {
RadioButtonInputs.Any(InputHasChanged);
}
private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool optional) {
private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool required) {
if (plzInput.Text.Length == 4) {
int plz = int.Parse(plzInput.Text);
ortInput.ItemsSource = Context.Postleitzahlen.Where(p => p.Plz == plz).ToHashSet();
@ -515,20 +537,20 @@ namespace Elwig.Windows {
} else {
Utils.ClearInputState(ortInput);
}
Valid[plzInput] = optional || (ortInput.ItemsSource != null);
Valid[plzInput] = !required || (ortInput.ItemsSource != null);
UpdateButtons();
}
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, ValidationResult> checker) {
InputTextChanged(input, optional, (tb, opt, ctx) => checker(tb, opt));
private void InputTextChanged(TextBox input, bool required, Func<TextBox, bool, ValidationResult> checker) {
InputTextChanged(input, required, (tb, opt, ctx) => checker(tb, opt));
}
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
InputTextChanged(input, optional, (tb, opt, ctx, m) => checker(tb, opt, ctx));
private void InputTextChanged(TextBox input, bool required, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
InputTextChanged(input, required, (tb, opt, ctx, m) => checker(tb, opt, ctx));
}
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, Member, ValidationResult> checker) {
var res = checker(input, optional, Context, (Member)MemberList.SelectedItem);
private void InputTextChanged(TextBox input, bool required, Func<TextBox, bool, AppDbContext, Member, ValidationResult> checker) {
var res = checker(input, required, Context, (Member)MemberList.SelectedItem);
Valid[input] = res.IsValid;
if (res.IsValid) {
if (InputHasChanged(input)) {
@ -542,16 +564,16 @@ namespace Elwig.Windows {
UpdateButtons();
}
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, optional, (tb, optional, ctx) => checker(tb, optional), msg);
private void InputLostFocus(TextBox input, bool required, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, required, (tb, required, ctx) => checker(tb, required), msg);
}
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, optional, (tb, optional, ctx, m) => checker(tb, optional, ctx), msg);
private void InputLostFocus(TextBox input, bool required, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, required, (tb, required, ctx, m) => checker(tb, required, ctx), msg);
}
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, Member?, ValidationResult> checker, string? msg = null) {
var res = checker(input, optional, Context, (Member)MemberList.SelectedItem);
private void InputLostFocus(TextBox input, bool required, Func<TextBox, bool, AppDbContext, Member?, ValidationResult> checker, string? msg = null) {
var res = checker(input, required, Context, (Member)MemberList.SelectedItem);
if (!res.IsValid)
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
}
@ -578,11 +600,17 @@ namespace Elwig.Windows {
private void TextBox_TextChanged(object sender, RoutedEventArgs evt) {
var input = (TextBox)sender;
if (SenderIsRequired(input) && input.Text.Length == 0) {
Valid[input] = false;
Utils.SetInputInvalid(input);
} else {
Valid[input] = true;
if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
}
}
UpdateButtons();
}
@ -599,99 +627,99 @@ namespace Elwig.Windows {
}
private void NumericInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, false, Validator.CheckNumeric);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckNumeric);
}
private void MgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, false, Validator.CheckMgNr);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckMgNr);
}
private void MgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, false, Validator.CheckMgNr);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckMgNr);
}
private void PredecessorMgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckPredecessorMgNr);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPredecessorMgNr);
}
private void PredecessorMgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPredecessorMgNr);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPredecessorMgNr);
}
private void BirthdayInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckPartialDate);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPartialDate);
}
private void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, false, Validator.CheckPlz);
UpdatePlz((TextBox)sender, OrtInput, false);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
UpdatePlz((TextBox)sender, OrtInput, SenderIsRequired(sender));
}
private void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPlz);
UpdatePlz((TextBox)sender, OrtInput, false);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
UpdatePlz((TextBox)sender, OrtInput, SenderIsRequired(sender));
}
private void PhoneNrInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckPhoneNumber);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPhoneNumber);
}
private void PhoneNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPhoneNumber);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPhoneNumber);
}
private void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckEmailAddress);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckEmailAddress);
}
private void EmailInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckEmailAddress);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckEmailAddress);
}
private void IbanInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckIban);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckIban);
}
private void IbanInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckIban);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckIban);
}
private void BicInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckBic);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckBic);
}
private void BicInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckBic);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckBic);
}
private void UstIdInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckUstId);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckUstId);
}
private void UstIdInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckUstId);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckUstId);
}
private void LfbisNrInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckLfbisNr);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckLfbisNr);
}
private void LfbisNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckLfbisNr);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckLfbisNr);
}
private void BillingPlzInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckPlz);
UpdatePlz((TextBox)sender, BillingOrtInput, true);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
UpdatePlz((TextBox)sender, BillingOrtInput, SenderIsRequired(sender));
}
private void BillingPlzInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPlz);
UpdatePlz((TextBox)sender, BillingOrtInput, true);
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
UpdatePlz((TextBox)sender, BillingOrtInput, SenderIsRequired(sender));
}
private void DateInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckDate);
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckDate);
}
}
}