Tagged: sitecore 9 forms

Sitecore 9.1 Forms: Conditional Logic

Conditional Logic is great new feature introduced in Sitecore 9.1 Forms. Basically you can add conditions to show/hide fields based on user input.

In this example below I set up a drop down and couple of sections that show/hide the section(you can go fields as well) based on user interaction.

My simple conditional logic form

Now I added the conditions to drop-down list by clicking on the Conditions section in the form elements pane.

Edit conditions to add new condition logic

Now are you wondering why the condition logic looks weird? Here is how it works –

  • All Condition are evaluated and applied in sequence.
  • Foreach condition, if the Condition matches, then apply selected action. Else, apply inverse of selected action.

Added the form to a web page and here is how my output looks –

When I chose 1, showed the Attendee #1 section
When I chose 2, showed both Attendees section

1. Dropdown Selection: Empty

  • Condition 1 Evaluation = False. Inverse selected action (Hide section1 and section2)
  • Condition 2 Evaluation = True. Apply selected action (Hide section1)

Results: Hide both section1 and section2

2. Dropdown Selection: 1

  • Condition 1 Evaluation = False. Inverse selected action (Hide section1 and section2)
  • Condition 2 Evaluation = False. Inverse selected action (Show section1)

Results: Show section1 and hide section2

3. Dropdown Selection: 2

  • Condition 1 Evaluation = True. Apply selected action (Show section1 and section2)
  • Condition 2 Evaluation = False. Inverse selected action (Show section1)

Results: Show both section1 and section2

Now you have the cool working conditional logic on the form. Any questions, please leave a comment.

Happy sitecoring!

4

Sitecore 9 Forms: The required anti-forgery cookie __requestVerificationToken is not present

Have you submitted a form and it returned with this error ‘The required anti-forgery cookie “__requestVerificationToken” is not present’? If so, here is the resolution.

  • This could be because of caching. Can you check if your rendering component or container/page level caching check box is checked? if so, unchecking the caching will fix the issue.
  • Also here is quick check you can do to see whether your form is cached – Inspect the form and check the _requestverificationtoken value like below and refresh the page – do you see same value now? if so, your form is in cache!
 <input name="__RequestVerificationToken" type="hidden" value="O0P3KYBqgEZXlksWnY3fhu0PptEB0a47rRGMgHNnF6lIL2S8tcts9z9NxdxIum7ANKMv95FZB275AeLTw6WxcOl4jCQ1">  

2

Sitecore 9 Forms: Redirecting to formbuilder on Submit?

Problem:

Have you submitted a form and it redirected to form builder url and displayed without styles? If so, here is the solution

Resolution:

  • Make sure to add these scripts to MVC OuterLayout.cshtml and reference it in MVCLayout.cshtml. More details, refer the documentation (Steps 3 and 4)
@using Sitecore.ExperienceForms.Mvc.Html  

@Html.RenderFormStyles()
@Html.RenderFormScripts() 

Any questions, please leave a comment. Happy sitecoring!

0

Sitecore 9 Forms: Custom Submit Action

One of most common scenario I came across in Sitecore 9 Form is to create your custom submit action. You can achieve this in three simple steps.

Step 1: Create submit action button in Sitecore

Create the custom action button under /sitecore/system/Settings/Forms/Submit Actions.

Step 2: Create code behind class

Create a class that inherits SubmitActionBase class and override the Execute method.

public class CustomSubmitAction : SubmitActionBase<string>
{
public CustomSubmitAction(ISubmitActionData submitActionData) : base(submitActionData)
{
}

public override void ExecuteAction(FormSubmitContext formSubmitContext, string parameters)
{
Assert.ArgumentNotNull((object)formSubmitContext, nameof(formSubmitContext));

if (this.TryParse(parameters, out string target))
{
try
{
if (this.Execute(target, formSubmitContext))
return;
}
catch (ArgumentNullException ex)
{
}
}
formSubmitContext.Errors.Add(new FormActionError()
{
ErrorMessage = this.SubmitActionData.ErrorMessage
});
}

protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull(data, nameof(data));
Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

//Prepare model here
var model = new EmailModel()
{
Email = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("EmailAddress")))
};

// Call any API service call

return true;
}

private static string GetValue(object field)
{
return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
}

protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}

Step 3: Set the custom submit action fields

Update the ModelType and Error Message on the button shown below.

Bind the custom submit action to Submit button like below.

Publish the button. The submit button on form should now trigger the custom save action! Any questions, let me know.

Happy sitecoring.

2

Sitecore 9 Forms: Success message


Sitecore 9 form feature is awesome and simple to work with. It is more like Visual Studio Toolbox and you can drag and drop the fields.

I had a requirement from Client asking for Ajax form and wanted to show Confirmation/Success message after the form is submitted. Out of the box, this is achievable with the Form.

Here is a small guide on how to create a form with confirmation message:

1. Select Forms from Dashboard.

2. Click Create button.
3. Select blank form.

4. Drag a page for main content.
5. Drag a another page for confirmation message.
6. Design the fields according to needs. Here I’m doing Email Signup form. You can add field validation and css-class as needed.

Please make sure you have these scripts in Layout.cshtml.

@Html.RenderFormStyles()
@Html.RenderFormScripts()

7. On the submit button, set the Navigation step to Next instead of submit. This does the trick to show the confirmation page.

7. Save the form.
8. Now you can see the form in Content Editor.

Don’t forget to publish the form and place it on a web page under MVC layout. Here is Sitecore documentation on Adding a form to webpage.

Happy Sitecoring.

0