1

I created a validation rule that should use regular expressions for checking if a field contains leading and trailing spaces. If it contains a space at the start or end of the field value, I expect it to throw an error on item save.

This is the regex pattern I created: ^\S.*\S$

The steps I took for creating the validation rule are as follows:

  • I created a validation rule item and called it "Trim Title", it's full path is /sitecore/system/Settings/Validation Rules/Field Rules/foldereName/Trim Title

  • In the Type field I referenced Sitecore.Data.Validators.FieldValidators.RegexValidator, Sitecore.Kernel

  • In the Parameters field I specified Pattern=^\S.*\S$&Text=Field "{0}" You cannot have a leading or trailing whitespace.

This is how my validation rule item looks in Content Editor:

Validation rule item

Then I went to my template "Headline Field" and under the Validation Rules section I selected my Trim Title validation rule in the fields Quick Action Bar, Validation Button, and Validation Bar. Selected field validation rules

I tried to test the field validation and it always fails. For example, if I enter some text with trailing and leading spaces it still saves the item and does not throw a validation error.

My idea was to create a regex rule similar to the Sitecore standard rule "Is Email". I replicated the same setup and simply changed the regex pattern but my rule does not work. What can be wrong with my validation rule?

Standard Sitecore "Is Email" validation rule

2
  • if this is your custom validator, shouldn't the Type field be "your.name.space,assembly.name" instead of Sitecore one? Have you tried putting breakpoint and debug to check if your code is hit? Check logicalfeed.com/posts/1198/… for detailed info.
    – Kamil C
    Jan 4, 2023 at 22:16
  • 1
    This question needs a better title, and some clarification on what exactly you're trying to do and how you're doing it. Is it a custom validator you built yourself, are you using the built-in validator functionality, etc.
    – Mark Cassidy
    Jan 5, 2023 at 1:47

1 Answer 1

0

I have tried replicating your setup on an empty Sitecore installation and can confirm that the custom regex validation rule works in exactly the same way as standard Sitecore regex rules.

For example, I applied the rule with regex pattern ^\S.*\S$ to the Sample Item template and it shows validation messages in Content Editor correctly:

Validation messages in Content Editor

However, this configuration does not prevent items from being saved and may be too subtle for content editors to notice. If this rule is critical and you want to stop users from saving items with this validation error, just add the parameter &Result=FatalError to the Parameters field on the validation rule item like this:

Validation rule configuration

Once this is added, Sitecore will not save incorrect field values and instead will throw an error message saying that fatal validation errors must be resolved before the item can be saved:

Error message popup on item save

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.