Validate input on child save & close

This commit is contained in:
kougyokugentou 2021-01-27 09:20:15 -08:00
parent 2b3c362ff4
commit 68e67516c0
2 changed files with 54 additions and 3 deletions

View File

@ -167,6 +167,7 @@
this.firstNameTextBox.Name = "firstNameTextBox";
this.firstNameTextBox.Size = new System.Drawing.Size(100, 22);
this.firstNameTextBox.TabIndex = 2;
this.firstNameTextBox.Validating += new System.ComponentModel.CancelEventHandler(this.String_TextBox_Validating);
//
// childBindingSource
//
@ -179,6 +180,7 @@
this.lastNameTextBox.Name = "lastNameTextBox";
this.lastNameTextBox.Size = new System.Drawing.Size(100, 22);
this.lastNameTextBox.TabIndex = 4;
this.lastNameTextBox.Validating += new System.ComponentModel.CancelEventHandler(this.String_TextBox_Validating);
//
// raceTextBox
//
@ -187,6 +189,7 @@
this.raceTextBox.Name = "raceTextBox";
this.raceTextBox.Size = new System.Drawing.Size(100, 22);
this.raceTextBox.TabIndex = 6;
this.raceTextBox.Validating += new System.ComponentModel.CancelEventHandler(this.String_TextBox_Validating);
//
// genderComboBox
//
@ -196,6 +199,7 @@
this.genderComboBox.Name = "genderComboBox";
this.genderComboBox.Size = new System.Drawing.Size(121, 24);
this.genderComboBox.TabIndex = 8;
this.genderComboBox.Validating += new System.ComponentModel.CancelEventHandler(this.genderComboBox_Validating);
//
// addressTextBox
//
@ -205,6 +209,7 @@
this.addressTextBox.Name = "addressTextBox";
this.addressTextBox.Size = new System.Drawing.Size(283, 79);
this.addressTextBox.TabIndex = 10;
this.addressTextBox.Validating += new System.ComponentModel.CancelEventHandler(this.String_TextBox_Validating);
//
// lblGuardians
//
@ -394,6 +399,7 @@
this.dOBMonthCalendar.MaxSelectionCount = 1;
this.dOBMonthCalendar.Name = "dOBMonthCalendar";
this.dOBMonthCalendar.TabIndex = 25;
this.dOBMonthCalendar.Validating += new System.ComponentModel.CancelEventHandler(this.dOBMonthCalendar_Validating);
//
// pic_openFileDialog
//

View File

@ -6,6 +6,7 @@ using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using GreatHomeChildcare.Models;
@ -312,10 +313,20 @@ namespace GreatHomeChildcare
*/
private void btnSave_Click(object sender, EventArgs e)
{
MessageBox.Show("Save and close");
return;
//Perform sanity check, ensure all data is filled except picture
this.Validate();
//TODO: Validate form
// Check to see if any control is in error.
foreach (Control c in errorProvider1.ContainerControl.Controls)
{
if (errorProvider1.GetError(c) != "")
{
MessageBox.Show("Child not saved due to errors on the form!", "Great Home Childcare", MessageBoxButtons.OK, MessageBoxIcon.None);
return;
}
}
//TODO: Ensure the child has at least one guardian.
//collect form and save to child object.
child.id = (int)idNumericUpDown.Value;
@ -376,5 +387,39 @@ namespace GreatHomeChildcare
return;
}
}
// Basic input validation on a string given a textbox control
// ensures only values a-z, with length two or greater.
private void String_TextBox_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (!Regex.IsMatch(tb.Text, "[A-Za-z]{2,}"))
errorProvider1.SetError(tb, "Enter a value a-z only of length two or longer.");
else
errorProvider1.SetError(tb, "");
}
//Ensures a gender was chosen.
private void genderComboBox_Validating(object sender, CancelEventArgs e)
{
ComboBox cb = (ComboBox)sender;
if (cb.SelectedIndex < 0)
{ errorProvider1.SetError(cb, "Select an item."); }
else
{ errorProvider1.SetError(cb, ""); }
}
//Basic validation on DOB, can't be today.
private void dOBMonthCalendar_Validating(object sender, CancelEventArgs e)
{
MonthCalendar mc = (MonthCalendar)sender;
if (mc.SelectionStart == DateTime.Today)
{ errorProvider1.SetError(mc, "Please choose the DOB."); }
else
{ errorProvider1.SetError(mc, ""); }
}
}
}