How to show field validation message on web part

Craig Vertigan 1 Reputation point
2020-09-25T05:01:01.88+00:00

I have created a SharePoint web part that dynamically creates a user input form to enter data into a list on a public facing website and then redirect the user to another page. The code finds all editable fields for the list and then loads them into a form using BaseFieldControl to create the controls. All the fields render with a required field validator text summary underneath them if they are set as required fields. So if the user doesn't enter something for a required field then SharePoint automatically adds the text "You must specify a value for this required field." underneath the field as such:
<span class="ms-formvalidation"><span role="alert">You must specify a value for this required field.</span><br></span>

But if a field also has a validation formula set such as a formula (ValidationFormula) of =LEN(Phone)>7 and a message (ValidationMessage) of "Phone numbers must be at least 8 numbers", then the form fails validation and has a SPListDataValidationException error, not a SPFieldValidationException, and the validation message for the field does not show up on the form anywhere.

This is how I create the form:
foreach (SPField field in list.Fields)
{
BaseFieldControl editControl = field.FieldRenderingControl;
editControl.ControlMode = SPControlMode.New;
editControl.ListId = list.ID;
editControl.FieldName = field.InternalName;
editControl.RenderContext = SPContext.GetContext(HttpContext.Current, list.DefaultView.ID, list.ID, web);
panel.Controls.Add(editControl);
}

Is there anyway I can get my web part to show that custom validation message I set in the list settings in the column?

All I have been able to do is capture the more generic message that I have put in the list validation message, using SPList.ValidationMessage instead of the specific SPField.ValidationMessage.

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,582 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Amos Wu-MSFT 4,051 Reputation points
    2020-09-25T07:52:59.35+00:00

    You could use BaseFieldControl.Validate function to validate and show the ValidationMessage in BaseFieldControl.ErrorMessage property.
    Reference:https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms434697(v=office.14)
    SPList validation is the verification of one or multiple list columns, SPField validation is the verification of the current column.I suggest not mixing them.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments