Sitecore | WFFM | Custom error message with details | on one page with the form

I have a web form for Marketer configured for one of my pages.

I have a Custom Submit Action written for it, as shown in the code snippet below -

public class **CustomFormSubmit : ISaveAction** { public void Execute(ID formid, AdaptedResultList fields, params object[] data) { try { **//var returnValue= Custom Logic to save form data // returns true or false** } catch (Exception ex) { Logger.Log(ex.Message + ":" + builder, ExceptionCategory.Error); throw; } } 

In my above web form - Success Mode - SuccessMode / Redirect, and I have a success page configured for it.

My requirement in the above scenario is to keep the user on the same page (with the form) if returnValue is false. (as shown in the code snippet above)

Can someone tell me in the above scenario how - how to save the user on the same page with the values ​​filled in the form so that the user can send it again. Product Details - 7.2 rev. 141226, Web forms for marketers 2.4 rev.140117

To add more information - I'm not sure how to return to my page instead of redirecting if false is returned in the code snippet above. As soon as the "Submit" button is pressed on this function, "Run" is called. How to return to the page - whether you need to redefine any function or configure something.

If any exception occurs when saving data - then the control returns to the same page with all the values ​​filled in by the user, with the message Save action with an error that is configured in Sitecore . Thus, my requirement will be to go to the form, as it happens in the case of Exception, when false is returned as a return value when saving data and put personalized error messages that can change every time, therefore it is not statically configured, rather dynamic. Thank you Saurabh

+4
source share
2 answers

One option is to redirect to the original page using a form.

Include the form to fill in the fields through the query string using the ReadQueryString property, using the view information of the Renderer form:

enter image description here

So, on false your Save Action you create a collection of query strings with the name of each field, as it appears in the form, followed by the user value.

The code below will cover all your fields and arrange them in a QueryString with its Name and Value field;

  string urlOfForm = HttpContext.Current.Request.Url.AbsolutePath; var queryString = new StringBuilder("?"); foreach (AdaptedControlResult field in fields) { queryString.Append(string.Format("{0}={1}&", field.FieldName, field.Value)); } urlOfForm = urlOfForm + queryString; HttpContext.Current.Response.Redirect(urlOfForm); 

Sitecore will then automatically fill in the appropriate fields with the values, fulfilling your requirements.

EDIT

I found that most of the thrown exceptions will return the user to the form with the values ​​populated. Then you can pass the reason for the refusal to write to CRM. Example below

  if (submitFailed) { throw new Exception("The email address entered already exists in our System"); } 

The complex dynamics then replaces the Save Action Failed Message to show this Exception Message . All the messages that I find in the user’s Save Action message show that the only real approach is to redirect through the custom Save Action to another page with another message. This is not suitable for your requirements.

I found the Args pipeline that you will need to fix FormSubmitFailedArgs and SubmitFailedArgs . For the former, the following change will be required

 public FormSubmitFailedArgs(ID formID, AdaptedResultList fields, ID actionFailed, Exception ex) : base(formID, actionFailed, ex) { this.Fields = fields; this.ErrorMessage = ex.Message; } 

and the latter will need

 public SubmitFailedArgs(ID formID, ID actionFailed, string errorMessage, Exception innerException) { this.FormID = formID; this.ActionFailed = actionFailed; this.ErrorMessage = innerException.Message; this.InnerException = innerException; } 

Location and style of message sending:

You need to find the sublayout file for FormRender, by default it is website\sitecore modules\Web\Web Forms for Marketers\Control\SitecoreSimpleFormAscx.ascx , there you will find a componentt named SubmitSummary that displays the send message, so move it to where you need .

Also note that it refers to the CssClass scfSubmitSummary , this is what you will need to configure in order to change the message style. This answer is already REALLY long, so I will not give a blow to the blow, how to change the style of this class, see here, for example, http://www.awareweb.com/awareblog/10-1-13-wffmguide

Pipeline Patrol

I went deeper into using the custom Args that we created to use the error message, you will need to control the Pipeline, which ultimately uses these Args, this is the Sitecore.Form.Core.Pipelines.FormSubmit.FormatMessage, Sitecore.Forms.Core processor Sitecore.Form.Core.Pipelines.FormSubmit.FormatMessage, Sitecore.Forms.Core in <errorSubmit> Pipeline.

From my research, this should not be a big effort, so its question on how to fix it can be changed if Sitecore.Forms.config directly or using patch:instead from the configuration file in the App_Config/Includes folder - see here for more details .

+2
source

One option is to create an action to validate a custom form. You can save the data here, although it would be better to check the data regarding your API here, and then save the data in a user save, simply because it seems more logical as to how WFFM should function.

 using Sitecore.Data; using Sitecore.Form.Core.Controls.Data; using Sitecore.Form.Core.Submit; using System; using System.Collections.Generic; namespace Custom.WFFM { public class CustomVerificationStep : BaseCheckAction { public string FailedMessage { get; set; } public override void Execute(ID formid, IEnumerable<ControlResult> fields) { // Call your API // You have access to the fields, so you can pass them through as parameters to your if needed bool flag = ServiceAPI.ValidateUserData(param1, param2, etc); if (!flag) { throw new Exception(string.Format(this.FailedMessage ?? "There was an error while verifying the data against the service call")); } } public override ActionState QueryState(ActionContext context) { return ActionState.DisabledSingleCall; } } } 

Create the appropriate verification action in the /sitecore/system/Modules/Web Forms for Marketers/Settings/Actions/Form Verification section:

Form Validation Action

You can change the error message by setting it in the Parameters field as <FailedMessage>Custom Failed Error Message</FailedMessage> .

Then add your validation step to your form:

Validation - Add to Form

If you need a different error message for each form, you can configure the error message to be displayed on the Error Messages tab.

Then the user will be returned to it without any called save actions and completed form fields.

+2
source

All Articles