/// <summary> /// Validate Notification data with errorProvider. /// </summary> /// <param name="notification">The Notification object that will be validated</param> /// <returns>Whether the notification is valid</returns> private bool ValidateNotification(Notification notification) { // using bottom-up to validate bool isValid = true; errorProvider.Clear(); // check Tags for (int i = tagBlocks.Count - 1; i >= 0; i--) { MessageBlock tagBlock = tagBlocks[i]; if (tagBlock.Tag.Type == TagType.UserInput && string.IsNullOrWhiteSpace(tagBlock.Input.Text)) { errorProvider.SetError(tagBlock.Input, "Please enter Tag " + tagBlock.Tag.Name); tagBlock.Input.Focus(); isValid = false; } } // check Message if (string.IsNullOrWhiteSpace(messageRichTextBox.Text)) { errorProvider.SetError(messageRichTextBox, "Please enter Message"); messageRichTextBox.Focus(); isValid = false; } // check Subject if (string.IsNullOrWhiteSpace(notification.Subject)) { errorProvider.SetError(subjectTextBox, "Please enter Subject"); subjectTextBox.Focus(); isValid = false; } // check Location if ( !sylvaniaCheckBox.Checked && !rockCreekCheckBox.Checked && !southeastCheckBox.Checked && !cascadeCheckBox.Checked ) { errorProvider.SetError(cascadeCheckBox, "Please select at least one Location"); sylvaniaCheckBox.Focus(); isValid = false; } return(isValid); }
/// <summary> /// Analyze and set messageBlocks that represent blocks of template content. /// </summary> /// <param name="message">message of template</param> private void ReloadBlocks(string message) { messageBlocks.Clear(); tagBlocks.Clear(); // Regex tested by https://regexr.com/4mokk // it splits {$foo} into blocks string[] blocks = Regex.Split(message, @"(\{\$.*?\})"); foreach (string block in blocks) { // Regex tested by https://regexr.com/4mokh // it matchs {$foo} bool isTag = Regex.IsMatch(block, @"^\{\$.*?\}$"); if (isTag) { string tagName = block.Substring(2, block.Length - 3); Tag tag = tags.Find(x => x.Name == tagName); // tag should never be null unless data in the DB is missing if (tag == null) { ShowErrorMessageBox(DataError, "Tag " + tagName + " is missing in DB"); messageBlocks.Clear(); tagBlocks.Clear(); return; } MessageBlock messageBlock = new MessageBlock { Tag = tag, IsTag = true, Message = null, Input = null }; messageBlocks.Add(messageBlock); tagBlocks.Add(messageBlock); } else { MessageBlock messageBlock = new MessageBlock { Tag = null, IsTag = false, Message = block, Input = null }; messageBlocks.Add(messageBlock); } } }