Change the default form submit error message

Abstract

Add your own error message for submit actions.

When you create a form, the Save Data submit action saves the form data to the database. By default, the Save Data submit action displays the same error message for all errors.

To change the default error message that is displayed for all errors:

  1. Navigate to /sitecore/system/Settings/Forms/Submit Actions.

  2. Click Save Data and in the Settings section, in the Error Message field, enter the new message.

    FORMS-save-data-error-message.PNG

If you want your error messages to be more specific, you can display a conditional error message.

To add a conditional form submit error message:

  1. To change a form submit error message from a save action programmatically, create a custom Save Data action that overrides the ExecuteAction method of the SubmitActionBase class. Here you can assign customized error messages that override the predefined submit error message.

                formSubmitContext.Errors.Add(new FormActionError
                {
                    ErrorMessage = _errorMessage
                });
    
  2. Inside your custom Save Data submit action class, you must manage the value of the error messages based on the different conditions. For example:

    class CustomSaveData:SaveData
        {
            private string _errorMessage;
            public CustomSaveData(ISubmitActionData submitActionData) : base(submitActionData)
            {
            }
    
            protected override bool SavePostedData(Guid formId, Guid sessionId, IList;IViewModel postedFields)
            {
                if (formId == Guid.Empty) //Condition 1
                {
                    _errorMessage = "Invalid form id!";
                    return false;
                }
    
                if (sessionId == Guid.Empty)// Condition 2
                {
                    _errorMessage = "Invalid session!";
                    return false;
                }
    
                try
                {
                    var formEntry = new FormEntry
                    {
                        Created = DateTime.UtcNow,
                        FormItemId = formId,
                        FormEntryId = sessionId,
                        Fields = new ListFieldData()
                    };
    
                    if (postedFields != null)
                    {
                        foreach (var postedField in postedFields)
                        {
                            AddFieldData(postedField, formEntry);
                        }
                    }
                    else
                    {
                        _errorMessage = "No data to save!"; //Condition 3
                        return false;
                    }
    
                    FormDataProvider.CreateEntry(formEntry);
    
                    return true;
                }
                catch (Exception exception)
                {
                    Logger.LogError(exception.Message, exception, this);
                    _errorMessage = exception.Message; //Condition 4
                    return false;
                }
            }
    
            public override void ExecuteAction(FormSubmitContext formSubmitContext, string parameters)
            {
                Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
    
                string data;
                if (TryParse(parameters, out data))
                {
                    try
                    {
                        if (Execute(data, formSubmitContext))
                        {
                            return;
                        }
                    }
                    catch (ArgumentNullException)
                    {
                    }
                }
    
                formSubmitContext.Errors.Add(new FormActionError
                {
                    ErrorMessage = _errorMessage
                });
            }
        }
        
  3. To replace the default Save Data action, navigate to /sitecore/system/Settings/Forms/Submit Actions, click Save Data and in the Settings section, in the Model Type field, replace the current value with your custom Save Data action class name.

    FORMS-save-data-mode-type.PNG

    You can also create a new submit action item.