public void clear() { this.name = ""; this.formControl = null; this.userEntryText = ""; this.databaseTable = ""; this.databaseTable = ""; this.successTextBox = null; this.dataFieldType = DataValidator.dataFieldType.NOTSET; }
public FormField(String Name, Object FormControl, String UserEntryText, String DatabaseTable, String DatabaseFieldName, TextBlock SuccessTextBox, DataValidator.dataFieldType DataFieldType) { this.name = Name; this.formControl = FormControl; this.userEntryText = UserEntryText; this.databaseTable = DatabaseTable; this.databaseFieldName = DatabaseFieldName; this.successTextBox = SuccessTextBox; this.dataFieldType = DataFieldType; }
// Method to validate the user input on the fly. // Is called whenever a user changes data in a form control. void formInputChanged(object sender, RoutedEventArgs e) { // Variables to store the Property and Value. string name = ""; string input = ""; // dataFieldType is an enum to choose the type of date you want to validate. // Different dataFieldTypes have different validation rules. // Set to "NOTSET" as default. DataValidator.dataFieldType type = DataValidator.dataFieldType.NOTSET; // The associated "Success" TextBlock, used to display a tick or cross depending on whether user input is valid. TextBlock successTextBlock = null; // Has the validation been a success? Set to false as default. bool validationSuccess = false; // Find out which kind of form control has called this method. // And extra the Property (name) and Value (input). if (sender is TextBox) { TextBox t = (TextBox)sender; name = t.Name; input = t.Text; } else if (sender is ComboBox) { ComboBox c = (ComboBox)sender; name = c.Name; input = ((ComboBoxItem)(c.SelectedItem)).Content.ToString(); } else if (sender is DatePicker) { DatePicker d = (DatePicker)sender; name = d.Name; input = d.SelectedDate.Value.Date.Date.ToShortDateString(); } else { // If it's not one of the above types of form control, just stop here. return; } // Escape this method if it hasn't found the Control Type by now. if (String.IsNullOrEmpty(name)) { return; } // Loop through the Fields List to assign the correct dataType, "Success" textBlock and the user input. for (int i = 0; i < fields.Count; i++) { if (fields[i].name.Equals(name)) { type = fields[i].dataFieldType; successTextBlock = fields[i].successTextBox; fields[i].userEntryText = input; } } // Find out the type of data the user has just entered ( ie firstname, dateOfBith, CreditCardNumber etc) if (type != DataValidator.dataFieldType.NOTSET) { // Check if the user input is valid, using the rules in the DataValidator.validateField method. validationSuccess = DataValidator.validateField(type, input); // If the data has been successfully validated, display the tick or a cross beside it. successTextBlock.Text = validationSuccess ? DataDelegate.SUCCESS : DataDelegate.FAIL; if (sender is TextBox) { TextBox tb = (TextBox)sender; tb.Background = validationSuccess ? new SolidColorBrush(SUCCESS_COLOR) : new SolidColorBrush(FAIL_COLOR); } if (sender is DatePicker) { DatePicker dp = (DatePicker)sender; dp.Background = validationSuccess ? new SolidColorBrush(SUCCESS_COLOR) : new SolidColorBrush(FAIL_COLOR); } } }
void formInputChanged(object sender, RoutedEventArgs e) { string name = ""; string input = ""; DataValidator.dataFieldType type = DataValidator.dataFieldType.NOTSET; TextBlock successTextBlock = null; bool validationSuccess = false; if (sender is TextBox) { TextBox t = (TextBox)sender; name = t.Name; input = t.Text; } else if (sender is ComboBox) { ComboBox c = (ComboBox)sender; name = c.Name; input = ((ComboBoxItem)(c.SelectedItem)).Content.ToString(); } else if (sender is DatePicker) { DatePicker d = (DatePicker)sender; name = d.Name; input = d.SelectedDate.Value.Date.Date.ToShortDateString(); } else { return; } // Escape this method if it hasn't found the Control Type by now. if (String.IsNullOrEmpty(name)) { return; } for (int i = 0; i < fields.Count; i++) { if (fields[i].name.Equals(name)) { type = fields[i].dataFieldType; successTextBlock = fields[i].successTextBox; fields[i].userEntryText = input; } } // Find out the type of data the user has just entered ( ie firstname, dateOfBith, CreditCardNumber etc) if (type != DataValidator.dataFieldType.NOTSET) { validationSuccess = DataValidator.validateField(type, input); successTextBlock.Text = validationSuccess ? SUCCESS : FAIL; if (sender is TextBox) { TextBox tb = (TextBox)sender; tb.Background = validationSuccess ? new SolidColorBrush(SUCCESS_COLOR) : new SolidColorBrush(FAIL_COLOR); } if (sender is DatePicker) { DatePicker dp = (DatePicker)sender; dp.Background = validationSuccess ? new SolidColorBrush(SUCCESS_COLOR) : new SolidColorBrush(FAIL_COLOR); } } }