Sitecore Forms: Validation messages

Forms validation: Tips and Quirks

In my last post I discussed some alterations to Sitecore form handling, specifically how to present forms on pages. In this post we will take a look at validation in Sitecore Forms and also at some quirks that you need to be aware of.

Validation

In Sitecore Forms, all validation settings are gathered in one place (SPOILER ALERT: They're not really... read on). This place is sitecore/system/settings/forms/validations:

 

Here you can specifiy the message as well as validation rules for specific field types. You can also create your own by following this official guide from Sitecore. And as per true Sitecore regime, these items can all be localized so that you can have different messages depending on what language the given site is currently running on.

Required message

As per my earlier spoiler alert, not all validation messages are covered by the above. The keen observer may note that at least one is missing, the most common one (probably) of all: The required field validation. This is managed in a different manner entirely, namely by adding a Dictionary Entry item under Dictionary with the appropriate key. See below:

The dictionary entry can be put anywhere you want under sitecore/system/dictionary, as long as it has the correct key it will work.

Email validation quirks

We experienced some weird behaviour when it came to email validation messages. If you look at the below validation messages, you'll note that the message is different depending on how much of a valid email address you have entered. Additionally, even the language differs. One message is localized, the other one is not.

The left message is the default jQuery.validation email message, which is the library that Sitecore Forms use for frontend form validation (just as WFFM did). And somehow, this has been missed and there is no possibility to localize or alter this validation message within Sitecore 9 as of revision 171219. I have confirmed this with Sitecore support.

Solution

So what you will need to do if you want to localize this message (and any other that might be using the default jQuer validation one) is to extend the validator messages. Download the jQuery validation source and locate the localization folder under src. You'll find individual scripts that extends the validation messages for roughly 60 different languages. However from what I can tell these scripts are exclusive, if you include more than one the last one will overwrite the others, so it's not possible to bundle all these scripts up to cover all your potential localization needs. You'll need to include only the specific one you need for the given language.

My solution to this problem is simply to copy the scripts from the localization-folder that you are interested in to ~\sitecore modules\Web\ExperienceForms\scripts, and then you can include any one you want to any given form by specifying it in the scripts field.

This does not enable editors to change the messages however, as they will have to be changed directly in the script file. It is of course possible to create your own template with messages and connect them to your frontend, and then you can just duplicate the message extension script logic for your own editor-controlled messages.

However, if you'd like to go about it another way, the below steps are what was provided to me by Sitecore Support when I reported the above. I'll warn you that I have not applied these steps myself, neither have I verified that they work or that it is the best way to achieve desired results. I'll leave that to you guys.

Happy hacking! (The steps below describe how to alter the swedish message, but I'm confident you can apply this to any language.)

sitecore_support_steps

15 Aug 2018, by Bonny Nilsson | 

Sitecore, Forms, Validation, Validation Messages