Add email and phone validators
This commit is contained in:
@ -23,42 +23,45 @@ namespace WGneu.Windows
|
||||
{
|
||||
public partial class MemberListWindow : Window
|
||||
{
|
||||
private bool isEditing = false;
|
||||
private bool isCreating = false;
|
||||
private List<string> textFilter = new();
|
||||
private static RoutedCommand controlF = new RoutedCommand();
|
||||
private readonly WgContext context = new();
|
||||
private bool IsEditing = false;
|
||||
private bool IsCreating = false;
|
||||
private List<string> TextFilter = new();
|
||||
private readonly Dictionary<TextBox, bool> Valid = new();
|
||||
private static readonly RoutedCommand CtrlF = new();
|
||||
private readonly WgContext Context = new();
|
||||
|
||||
public MemberListWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
CtrlF.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control));
|
||||
CommandBindings.Add(new CommandBinding(CtrlF, FocusSearchInput));
|
||||
foreach (var tb in Utils.FindVisualChilds<TextBox>(this))
|
||||
if (tb.Name != "SearchInput") Valid[tb] = true;
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
RefreshMemberList();
|
||||
BranchInput.ItemsSource = context.Branches.OrderBy(b => b.Name).ToList();
|
||||
DefaultKgInput.ItemsSource = context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList();
|
||||
controlF.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control));
|
||||
CommandBindings.Add(new CommandBinding(controlF, FocusSearchInput));
|
||||
BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList();
|
||||
DefaultKgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList();
|
||||
}
|
||||
|
||||
protected override void OnClosing(CancelEventArgs e)
|
||||
{
|
||||
context.Dispose();
|
||||
Context.Dispose();
|
||||
base.OnClosing(e);
|
||||
}
|
||||
|
||||
private int CountMatchesInMember(Member m)
|
||||
{
|
||||
if (textFilter.Count == 0) return 0;
|
||||
if (TextFilter.Count == 0) return 0;
|
||||
string[] check = new string[] { m.MgNr.ToString(), m.FamilyName.ToLower(), m.GivenName.ToLower(), m.DefaultKg.Name.ToLower() };
|
||||
int i = 0;
|
||||
foreach (string c in check)
|
||||
{
|
||||
if (textFilter.Any(f => c == f))
|
||||
if (TextFilter.Any(f => c == f))
|
||||
i += 10;
|
||||
else if (textFilter.Any(f => c.Contains(f)))
|
||||
else if (TextFilter.Any(f => c.Contains(f)))
|
||||
i += 1;
|
||||
}
|
||||
return i;
|
||||
@ -66,10 +69,10 @@ namespace WGneu.Windows
|
||||
|
||||
private void RefreshMemberList()
|
||||
{
|
||||
context.Members.Load();
|
||||
List<Member> members = context.Members.OrderBy(m => m.FamilyName + " " + m.GivenName).ToList();
|
||||
Context.Members.Load();
|
||||
List<Member> members = Context.Members.OrderBy(m => m.FamilyName + " " + m.GivenName).ToList();
|
||||
|
||||
if (textFilter.Count > 0)
|
||||
if (TextFilter.Count > 0)
|
||||
{
|
||||
members = members
|
||||
.ToDictionary(m => m, m => CountMatchesInMember(m))
|
||||
@ -118,7 +121,7 @@ namespace WGneu.Windows
|
||||
if (PlzInput.Text.Length == 4 && PlzInput.Text.All(char.IsDigit))
|
||||
{
|
||||
int plz = int.Parse(PlzInput.Text);
|
||||
var o = context.Postleitzahlen.Where(p => p.Plz == plz).ToHashSet();
|
||||
var o = Context.Postleitzahlen.Where(p => p.Plz == plz).ToHashSet();
|
||||
OrtInput.ItemsSource = o;
|
||||
OrtInput.SelectedItem = null;
|
||||
}
|
||||
@ -126,7 +129,7 @@ namespace WGneu.Windows
|
||||
|
||||
private void NewMemberButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
isCreating = true;
|
||||
IsCreating = true;
|
||||
MemberList.IsEnabled = false;
|
||||
InitInputs();
|
||||
HideNewEditDeleteButtons();
|
||||
@ -139,7 +142,7 @@ namespace WGneu.Windows
|
||||
if (MemberList.SelectedItem == null)
|
||||
return;
|
||||
|
||||
isEditing = true;
|
||||
IsEditing = true;
|
||||
MemberList.IsEnabled = false;
|
||||
|
||||
HideNewEditDeleteButtons();
|
||||
@ -157,8 +160,8 @@ namespace WGneu.Windows
|
||||
"Mitglied löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
||||
if (r == MessageBoxResult.Yes)
|
||||
{
|
||||
context.Remove(m);
|
||||
context.SaveChanges();
|
||||
Context.Remove(m);
|
||||
Context.SaveChanges();
|
||||
RefreshMemberList();
|
||||
}
|
||||
}
|
||||
@ -167,42 +170,70 @@ namespace WGneu.Windows
|
||||
{
|
||||
// TODO only allow to click button, if values were checked
|
||||
|
||||
Member? m = null;
|
||||
if (isEditing)
|
||||
Member? m = new();
|
||||
if (IsEditing)
|
||||
m = (Member)MemberList.SelectedItem;
|
||||
else if (isCreating)
|
||||
else if (IsCreating)
|
||||
m = new();
|
||||
|
||||
int newMgNr = int.Parse(MgNrInput.Text);
|
||||
m.Prefix = (PrefixInput.Text == "") ? null : PrefixInput.Text;
|
||||
m.GivenName = GivenNameInput.Text;
|
||||
m.FamilyName = FamilyNameInput.Text;
|
||||
m.ZwstId = ((Branch)BranchInput.SelectedItem).ZwstId;
|
||||
m.Suffix = (SuffixInput.Text == "") ? null : SuffixInput.Text;
|
||||
m.Birthday = (BirthdayInput.Text == "") ? null : string.Join("-", BirthdayInput.Text.Split(".").Reverse());
|
||||
m.CountryCode = "AT";
|
||||
m.PostalDestId = ((AT_Plz)OrtInput.SelectedItem).Id;
|
||||
m.PostalDest = context.PostalDestinations.Find(m.CountryCode, m.PostalDestId);
|
||||
m.PostalDest = Context.PostalDestinations.Find(m.CountryCode, m.PostalDestId);
|
||||
m.Address = AddressInput.Text;
|
||||
|
||||
m.Email = (EmailInput.Text == "") ? null : EmailInput.Text;
|
||||
m.PhoneLandline = (PhoneLandlineInput.Text == "") ? null : PhoneLandlineInput.Text.Replace(" ", "");
|
||||
m.PhoneMobile1 = (PhoneMobile1Input.Text == "") ? null : PhoneMobile1Input.Text.Replace(" ", "");
|
||||
m.PhoneMobile2 = (PhoneMobile2Input.Text == "") ? null : PhoneMobile2Input.Text.Replace(" ", "");
|
||||
|
||||
m.Iban = (IbanInput.Text == "") ? null : IbanInput.Text.Replace(" ", "");
|
||||
m.Bic = (BicInput.Text == "") ? null : BicInput.Text;
|
||||
|
||||
m.UstId = (UstIdInput.Text == "") ? null : UstIdInput.Text;
|
||||
m.LfbisNr = (LfbisNrInput.Text == "") ? null : LfbisNrInput.Text;
|
||||
m.Buchführend = BuchführendInput.IsChecked ?? false;
|
||||
|
||||
// TODO Rechnungsadresse
|
||||
|
||||
m.EntryDate = (EntryDateInput.Text == "") ? null : string.Join("-", EntryDateInput.Text.Split(".").Reverse());
|
||||
m.ExitDate = (ExitDateInput.Text == "") ? null : string.Join("-", ExitDateInput.Text.Split(".").Reverse());
|
||||
m.BusinessShares = (BusinessSharesInput.Text == "") ? 0 : int.Parse(BusinessSharesInput.Text);
|
||||
m.Active = ActiveInput.IsChecked ?? false;
|
||||
m.VollLieferant = VollLieferantInput.IsChecked ?? false;
|
||||
m.Funktionär = FunkionärInput.IsChecked ?? false;
|
||||
m.ZwstId = ((Branch)BranchInput.SelectedItem).ZwstId;
|
||||
m.DefaultKgNr = ((AT_Kg)DefaultKgInput.SelectedItem).KgNr;
|
||||
m.Comment = (CommentInput.Text == "") ? null : CommentInput.Text;
|
||||
if (ContactPostInput.IsChecked ?? false) m.DefaultContact = "post";
|
||||
if (ContactEmailInput.IsChecked ?? false) m.DefaultContact = "email";
|
||||
// TODO Buchhaltungskonto
|
||||
|
||||
try
|
||||
{
|
||||
if (isEditing)
|
||||
context.Update(m);
|
||||
else if (isCreating)
|
||||
context.Add(m);
|
||||
context.SaveChanges();
|
||||
if (IsEditing)
|
||||
Context.Update(m);
|
||||
else if (IsCreating)
|
||||
Context.Add(m);
|
||||
Context.SaveChanges();
|
||||
|
||||
if (newMgNr != m.MgNr)
|
||||
context.Database.ExecuteSql($"UPDATE member SET mgnr = {newMgNr} WHERE mgnr = {m.MgNr}");
|
||||
Context.Database.ExecuteSql($"UPDATE member SET mgnr = {newMgNr} WHERE mgnr = {m.MgNr}");
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message.ToString(),
|
||||
"Mitglied aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message;
|
||||
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
|
||||
MessageBox.Show(str, "Mitglied aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
|
||||
isEditing = false;
|
||||
isCreating = false;
|
||||
IsEditing = false;
|
||||
IsCreating = false;
|
||||
MemberList.IsEnabled = true;
|
||||
HideSaveResetCancelButtons();
|
||||
ShowNewEditDeleteButtons();
|
||||
@ -212,16 +243,16 @@ namespace WGneu.Windows
|
||||
|
||||
private void ResetButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (isEditing)
|
||||
if (IsEditing)
|
||||
RefreshInputs();
|
||||
else if (isCreating)
|
||||
else if (IsCreating)
|
||||
InitInputs();
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
isEditing = false;
|
||||
isCreating = false;
|
||||
IsEditing = false;
|
||||
IsCreating = false;
|
||||
MemberList.IsEnabled = true;
|
||||
HideSaveResetCancelButtons();
|
||||
ShowNewEditDeleteButtons();
|
||||
@ -231,14 +262,13 @@ namespace WGneu.Windows
|
||||
|
||||
private void SearchInput_TextChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// TODO implement STRG+F
|
||||
textFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(s => s != "");
|
||||
TextFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(s => s != "");
|
||||
RefreshMemberList();
|
||||
}
|
||||
|
||||
private void FocusSearchInput(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!isEditing && !isCreating)
|
||||
if (!IsEditing && !IsCreating)
|
||||
{
|
||||
SearchInput.Focus();
|
||||
SearchInput.SelectAll();
|
||||
@ -247,8 +277,8 @@ namespace WGneu.Windows
|
||||
|
||||
private int NextMgNr()
|
||||
{
|
||||
int c = context.Members.Select(m => m.MgNr).Min();
|
||||
context.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; });
|
||||
int c = Context.Members.Select(m => m.MgNr).Min();
|
||||
Context.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; });
|
||||
return c + 1;
|
||||
}
|
||||
|
||||
@ -319,24 +349,55 @@ namespace WGneu.Windows
|
||||
private void FillInputs(Member m)
|
||||
{
|
||||
MgNrInput.Text = m.MgNr.ToString();
|
||||
PredecessorMgNrInput.Text = m.PredecessorMgNr.ToString();
|
||||
PrefixInput.Text = m.Prefix;
|
||||
GivenNameInput.Text = m.GivenName;
|
||||
FamilyNameInput.Text = m.FamilyName;
|
||||
BranchInput.SelectedItem = m.Branch;
|
||||
DefaultKgInput.SelectedItem = m.DefaultKg;
|
||||
SuffixInput.Text = m.Suffix;
|
||||
BirthdayInput.Text = (m.Birthday != null) ? string.Join(".", m.Birthday.Split("-").Reverse()) : null;
|
||||
AddressInput.Text = m.Address;
|
||||
|
||||
AT_Plz? p = m.PostalDest.Plz(context);
|
||||
AT_Plz? p = m.PostalDest.Plz(Context);
|
||||
if (p != null)
|
||||
{
|
||||
PlzInput.Text = p.Plz.ToString();
|
||||
OrtInput.ItemsSource = p.Orte(context);
|
||||
OrtInput.ItemsSource = p.Orte(Context);
|
||||
OrtInput.SelectedItem = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
PlzInput.Text = null;
|
||||
OrtInput.ItemsSource = null;
|
||||
OrtInput.SelectedItem = null;
|
||||
}
|
||||
|
||||
EmailInput.Text = m.Email;
|
||||
PhoneLandlineInput.Text = m.PhoneLandline;
|
||||
PhoneMobile1Input.Text = m.PhoneMobile1;
|
||||
PhoneMobile2Input.Text = m.PhoneMobile2;
|
||||
|
||||
IbanInput.Text = m.Iban;
|
||||
BicInput.Text = m.Bic;
|
||||
|
||||
UstIdInput.Text = m.UstId;
|
||||
LfbisNrInput.Text = m.LfbisNr;
|
||||
BuchführendInput.IsChecked = m.Buchführend;
|
||||
|
||||
// TODO Rechnungsadresse
|
||||
|
||||
EntryDateInput.Text = (m.EntryDate != null) ? string.Join(".", m.EntryDate.Split("-").Reverse()) : null;
|
||||
ExitDateInput.Text = (m.ExitDate != null) ? string.Join(".", m.ExitDate.Split("-").Reverse()) : null;
|
||||
BusinessSharesInput.Text = m.BusinessShares.ToString();
|
||||
BranchInput.SelectedItem = m.Branch;
|
||||
DefaultKgInput.SelectedItem = m.DefaultKg;
|
||||
CommentInput.Text = m.Comment;
|
||||
ActiveInput.IsChecked = m.Active;
|
||||
VollLieferantInput.IsChecked = m.VollLieferant;
|
||||
FunkionärInput.IsChecked = m.Funktionär;
|
||||
switch (m.DefaultContact)
|
||||
{
|
||||
case "post": ContactPostInput.IsChecked = true; break;
|
||||
case "email": ContactEmailInput.IsChecked = true; break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearInputs()
|
||||
@ -345,20 +406,82 @@ namespace WGneu.Windows
|
||||
if (tb.Name != "SearchInput") tb.Text = "";
|
||||
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this))
|
||||
cb.SelectedItem = null;
|
||||
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this))
|
||||
cb.IsChecked = false;
|
||||
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this))
|
||||
rb.IsChecked = false;
|
||||
}
|
||||
|
||||
private bool IsValid()
|
||||
{
|
||||
return Valid.All(kv => kv.Value) &&
|
||||
Utils.FindVisualChilds<ComboBox>(this).All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
|
||||
}
|
||||
|
||||
private void UpdateButtons()
|
||||
{
|
||||
if (!IsEditing && !IsCreating) return;
|
||||
bool ch = HasChanged(), v = IsValid();
|
||||
ResetButton.IsEnabled = (ch);
|
||||
SaveButton.IsEnabled = (v && ch);
|
||||
}
|
||||
|
||||
private bool HasChanged()
|
||||
{
|
||||
return true; // TODO
|
||||
}
|
||||
|
||||
private void InputTextChanged(TextBox input, Func<TextBox, ValidationResult> checker)
|
||||
{
|
||||
var res = checker(input);
|
||||
Valid[input] = res.IsValid;
|
||||
if (res.IsValid)
|
||||
Validator.SetInputValid(input);
|
||||
else
|
||||
Validator.SetInputInvalid(input);
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void InputLostFocus(TextBox input, Func<TextBox, ValidationResult> checker, string? msg)
|
||||
{
|
||||
var res = checker(input);
|
||||
if (!res.IsValid)
|
||||
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
}
|
||||
|
||||
private void ComboBox_SelectionChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void PhoneNrInput_TextChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InputTextChanged((TextBox)sender, Validator.CheckPhoneNumber);
|
||||
}
|
||||
|
||||
private void PhoneNrInput_LostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InputLostFocus((TextBox)sender, Validator.CheckPhoneNumber, null);
|
||||
}
|
||||
|
||||
private void EmailInput_TextChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InputTextChanged((TextBox)sender, Validator.CheckEmailAddress);
|
||||
}
|
||||
|
||||
private void EmailInput_LostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InputLostFocus((TextBox)sender, Validator.CheckEmailAddress, null);
|
||||
}
|
||||
|
||||
private void LfbisNrInput_TextChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var res = Validator.CheckLfbisNr(LfbisNrInput);
|
||||
if (res.IsValid) Validator.SetInputValid(LfbisNrInput);
|
||||
else Validator.SetInputInvalid(LfbisNrInput);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckLfbisNr);
|
||||
}
|
||||
|
||||
private void LfbisNrInput_LostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var res = Validator.CheckLfbisNr(LfbisNrInput);
|
||||
if (!res.IsValid)
|
||||
MessageBox.Show(res.ErrorContent.ToString(), "Betriebsnummer ungültig", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckLfbisNr, "Betriebsnummer ungültig");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user