Handle submit action errors for forms
When a form submit action fails to run, the default error message associated with the submit action displays on the form and the user is unable to submit the form. You can change the default error message associated with a submit action to best suit your needs.
You can also hide the default error message that is associated with a submit action. This might be important in a production environment where you do not want to display error messages or prevent a form from being submitted. For example, the Trigger Goal submit action might fail when xDB Tracker is unavailable due to inadequate user permissions but, in this case, you still want the user to be able to proceed with form submission.
Default error messages are recorded in the log file (App_Data\logs\log.<date>.<time>.txt). Where the Error Message field for a submit action is blank, the error message is still recorded in the log file (for example, the log entry might read the goal <goal item ID> cannot be registered because the Tracker is disabled or not initialized.). In addition, some submit actions log runtime exceptions, including their stack trace.
Change the default error message for a submit action error
This procedure shows you how to change the default error message for a submit action, using the Save Data action as an example. When you create a form, the Save Data submit action saves the form data to the Forms database.
To change the default error message:
-
In the Content Editor, go to
/sitecore/System/Settings/Forms/Submit Actions. -
In the content tree, click Save Data and, in the Settings section, in the Error Message field, enter the new message and save your changes.
ImportantTo hide the default error message and permit the form to be submitted, leave the Error Message field blank.
Create a conditional form submit error message
If you want a more specific error message, you can create a custom submit action to conditionally set the error message and display the default error message if the conditions are not met.
To create a conditional error message:
-
To change a form submit error message from a save action programmatically, create a custom Save Data action that overrides the
ExecuteActionmethod of theSubmitActionBaseclass. Here you can assign customized error messages that override the predefined submit error message.RequestResponseformSubmitContext.Errors.Add(new FormActionError { ErrorMessage = _errorMessage }); -
Inside your custom
Save Data submit actionclass, you must manage the value of the error messages based on the different conditions. For example:RequestResponseclass 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 }); } } -
To replace the default Save Data action, go 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 customSave Data actionclass name.You can also create a new submit action item.