InputIssueList GetIssuesInFieldName(string fieldName)
        {
            InputIssueList issues = new InputIssueList();

            if (!IsAlpha(fieldName))
            {
                issues.Add(Warning.NonAlphaInFieldName);
                if (!IsComposedOf(fieldName, _validNameChars))
                {
                    issues.Add(Error.InvalidCharsInFieldName);
                }
            }
            if (fieldName.Length == 0)
            {
                issues.Add(Error.FieldNameEmpty);
            }
            else
            {
                if (!fieldName.ToLower().StartsWith(fieldName.Substring(0, 1)))
                {
                    issues.Add(Error.FieldNameNotCamelCase);
                }
            }
            return(issues);
        }
        InputIssueList GetIssuesInFieldDefaultValue(string fieldDefaultValue)
        {
            InputIssueList issues = new InputIssueList();

            if (!IsComposedOf(fieldDefaultValue, _validDefaultValueChars))
            {
                issues.Add(Warning.InvalidCharsInFieldDefaultValue);
            }
            return(issues);
        }
        void DisplayFirstIssue(InputIssueList issueList, Label label)
        {
            InputIssue issue = issueList.FirstIssueOrNull;

            if (issue is null)
            {
                label.Text = "";
            }
            else
            {
                issue.DisplayInControl(label);
            }
        }
 InputIssueList ValidateInputFieldEnumCode()
 {
     if (listViewFields.SelectedItems.Count != 0)
     {
         _inputIssuesEditFieldEnumCode = GetIssuesInFieldEnumCode(GetColumn(listViewFields.SelectedItems[0], colFieldEnumCode).Text);
     }
     else
     {
         _inputIssuesEditFieldEnumCode.Clear();
     }
     DisplayFirstIssue(_inputIssuesEditFieldEnumCode, lblIssueEditFieldEnumCode);
     return(_inputIssuesEditFieldEnumCode);
 }
        InputIssueList GetIssuesInFieldEnumCode(string fieldEnumCode)
        {
            InputIssueList issues = new InputIssueList();

            if (!IsComposedOf(fieldEnumCode, _alphabet + "_"))
            {
                issues.Add(Warning.NonAlphaInFieldEnumCode);
                if (!IsComposedOf(fieldEnumCode, _validEnumCodeChars))
                {
                    issues.Add(Error.InvalidCharsInFieldEnumCode);
                }
            }
            if (!fieldEnumCode.Equals(fieldEnumCode.ToUpper()))
            {
                issues.Add(Error.FieldEnumCodeNotUpperCase);
            }
            return(issues);
        }
        InputIssueList GetIssuesInFieldDisp(string fieldDisp)
        {
            InputIssueList issues = new InputIssueList();

            if (!IsComposedOf(fieldDisp, _alphabet + " "))
            {
                issues.Add(Warning.NonAlphaInFieldDisp);
                if (!IsComposedOf(fieldDisp, _validDispChars))
                {
                    issues.Add(Warning.InvalidCharsInFieldDisp);
                }
            }
            if (!fieldDisp.Equals(fieldDisp.ToLower()))
            {
                issues.Add(Error.FieldDispNotLowerCase);
            }
            return(issues);
        }
        InputIssueList ValidateInputFields()
        {
            _inputIssuesFields.Clear();

            // Must be at least one field
            if (listViewFields.Items.Count == 0)
            {
                _inputIssuesFields.Add(Error.TooFewFields);
            }

            // Every field name and enum code must be unique
            for (int i = 0; i < listViewFields.Items.Count; i++)
            {
                for (int j = i + 1; j < listViewFields.Items.Count; j++)
                {
                    if (GetColumn(listViewFields.Items[i], colFieldName).Text.Equals(GetColumn(listViewFields.Items[j], colFieldName)))
                    {
                        _inputIssuesFields.Add(Error.NonUniqueFieldName);
                    }
                    if (GetColumn(listViewFields.Items[i], colFieldEnumCode).Text.Equals(GetColumn(listViewFields.Items[j], colFieldEnumCode)))
                    {
                        _inputIssuesFields.Add(Error.NonUniqueFieldEnumCode);
                    }
                }
            }

            // There must be no issues with any of the particular fields
            foreach (ListViewItem lvi in listViewFields.Items)
            {
                _inputIssuesFields += GetIssuesInFieldType(GetColumn(lvi, colFieldType).Text);
                _inputIssuesFields += GetIssuesInFieldName(GetColumn(lvi, colFieldName).Text);
                _inputIssuesFields += GetIssuesInFieldDisp(GetColumn(lvi, colFieldDisp).Text);
                _inputIssuesFields += GetIssuesInFieldDefaultValue(GetColumn(lvi, colFieldDefaultValue).Text);
                _inputIssuesFields += GetIssuesInFieldEnumCode(GetColumn(lvi, colFieldEnumCode).Text);
            }

            DisplayFirstIssue(_inputIssuesFields, lblIssueFields);

            return(_inputIssuesFields);
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            InputIssueList issueList = new InputIssueList();

            issueList += ValidateInputName();
            issueList += ValidateInputDispSingular();
            issueList += ValidateInputDispPlural();
            issueList += ValidateInputEnumCode();
            issueList += ValidateInputFields();

            if (issueList.ContainsErrors())
            {
                string message = "Your input generated the following errors. You must fix all these errors before you can create a new question type.";
                foreach (InputIssue error in issueList.Errors)
                {
                    message += "\n\n" + error.Message;
                }
                MessageBox.Show(message, "Error", MessageBoxButtons.OK);
                return;
            }

            if (issueList.ContainsWarnings())
            {
                string message = "Your input generated the following warnings:";
                foreach (InputIssue warning in issueList.Warnings)
                {
                    message += "\n\n" + warning.Message;
                }
                message += "\n\n\nIt is strongly advised you go back and fix these warnings before proceeding.";
                if (MessageBox.Show(message, "Warning", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    UpdateAndClose();
                }
                return;
            }

            UpdateAndClose();
        }