Making errors on forms look much better

Tim Chalklen asked on July 2, 2014 08:43

With the default set up of a form, if any fields are missing when the user submits it, the display is really basic - i.e. the text 'Error The entered values cannot be saved. Please see the fields below for details' is displayed at the top with no indication of which fields are invalid at all.

Has anyone found a simple way to make this process come in line with latest best practice on form submission errors - e.g. highlighting the offending field in red, listing which fields are invalid and why...?

Recent Answers


Joshua Adams answered on July 2, 2014 09:39

You could control the css classes through properties on the form. If you have access to the code of the page, you can actually set the fielderrorcssclass through a property on the form. Then you could control the styling on your own. Or you could override their css class for the errorlabel.

0 votesVote for this answer Mark as a Correct answer

Yehuda Lando answered on July 2, 2014 22:39

I made my own form controls that utilize both html5 validation (mostly for the css selectors :valid and :invalid) and asp.net client side validation. I added a RequiredFieldValidator and RegularExpressionValidator to my form control to handle validation according to the rules in the form settings.

I also added the label to the form control so I can show/hide the required mark.

1 votesVote for this answer Mark as a Correct answer

Yehuda Lando answered on July 2, 2014 22:42

This is the code from my text box controls, which I added at the end of the OnLoad method:

var fieldInfo = this.FieldInfo;

Label.Text = fieldInfo.Caption;

if (!fieldInfo.AllowEmpty)
{
    var errorMessage = fieldInfo.GetPropertyValue(FormFieldPropertyEnum.ValidationErrorMessage);
    Label.ShowRequiredMark = true;
    rfvTextbox.Text = string.IsNullOrEmpty(errorMessage) ? "This field is required" : errorMessage; ;
    rfvTextbox.Enabled = true;
    textbox.Attributes.Add("required", string.Empty);
}
else
{
    rfvTextbox.Enabled = false;
}
if (fieldInfo.FieldMacroRules.Any())
{
    var macroRule = fieldInfo.FieldMacroRules.First();

    if (!string.IsNullOrEmpty(fieldInfo.RegularExpression))
    {
        var actual = Regex.Unescape(fieldInfo.RegularExpression);

        textbox.Attributes.Add("pattern", actual);
        revTextbox.ValidationExpression = actual;
        revTextbox.ErrorMessage = string.IsNullOrEmpty(macroRule.ErrorMessage) ? fieldInfo.GetPropertyValue(FormFieldPropertyEnum.ValidationErrorMessage) : macroRule.ErrorMessage;
    }
}
else
{
    revTextbox.Enabled = false;
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.