Thursday, November 2, 2017

Validate Sitecore Web Forms for Marketers Telephone field for Length

I know Sitecore Web Forms for Marketers module will be deprecated with Sitecore 9 release, with the introduction of Sitecore Forms module.

Nevertheless I thought to write a quick post on a recent simple issue that I had to try and find a solution for.

In one of my current projects, I had to add a length validation to Telephone field in WFFM form.

So, I just added another validation to Telephone field definition item in WFFM (see Image-1).

Image-1 : Adding "count chars" validation to Telephone field

Then I tested the form. Even thought validation worked, it was considering 0 as Minimum Length & 256 as Maximum Length of that field value. There were the default values define for Single-line text field class in Sitecore.

Even though we can see Minimum Length & Maximum Length fields for WFFM Single-line text field (see in image-2), WFFM Design interface for Telephone field does NOT display any Minimum Length & Maximum Length field (see image-3).


Image-2 : WFFM Single-line Text Field Design Interface

Image-3 : WFFM Telephone Field Design Interface


But, If you look at the Telephone field definition in WFFM (see Image-4), you can see it is using "Sitecore.Forms.Mvc.ViewModels.Fields.TelephoneField,Sitecore.Forms.Mvc".

Image-4 : Telephone Field Definition Item


So, once I looked into the code base for that class by decoding the Sitecore.Forms.Mvc.dll, I could see it was extending the Single-line text field class (see Image-5)

Image-5 : TelephoneField Class


So, as you know, SingleLineTextField is created to use Maximum Length & Minimum Length properties.

Finally, to get these length validations to work, all I had to do is add following parameters to "Parameters" field of that Telephone field item (see Image-6)

<MinLength>10</MinLength><MaxLength>15</MaxLength>



That was it. After that, Telephone field validation works as I needed with 10 characters as Minimum Length and 15 characters as Maximum length.

Hope this helps to someone ! :-)

1 comment:

  1. This is not gonna work if multiple versions of web pages, as the field of parameters is shared, unversioned. So only one version will work for all.

    ReplyDelete