1

I have been successful in creating a field validation rule where the Title should not have hyphens by using a regex validation. That is working as expected. Now, I need to validate the items while creating it like the item's Name shouldn't have hyphens in it. I have created an Item Validation rule as shown in below image.

enter image description here

But, the rule is not working as expected. I tried in all types of "Validation Rules" and the items still can be created with hyphens in it. Can anyone correct me if I am missing anything.

4
  • See sitecore.stackexchange.com/questions/20404/…
    – Marek Musielak
    Commented Feb 14, 2023 at 14:23
  • @MarekMusielak The config change works like a charm. But is there a way to restrict the [Item Name validation] to only content items (excluding media library)?
    – Yeshwanth
    Commented Feb 22, 2023 at 10:19
  • no, no way to restrict to content items only
    – Marek Musielak
    Commented Feb 22, 2023 at 11:04
  • Ok. Thank you for the quick response.
    – Yeshwanth
    Commented Feb 22, 2023 at 11:37

2 Answers 2

1

The issue I can see is that you are using FieldValidators under the type i.e. Sitecore.Data.Validators.FieldValidators.RegexValidator,Sitecore.Kernel

And the logic for this validator checks for the field.

 string controlValidationValue = this.ControlValidationValue;
 if (string.IsNullOrEmpty(controlValidationValue))
        return ValidatorResult.Valid;
 string parameter = this.Parameters["Pattern"];
 if (string.IsNullOrEmpty(parameter) || new Regex(parameter, RegexOptions.IgnoreCase).IsMatch(controlValidationValue))
        return ValidatorResult.Valid;
 this.Text = this.GetText("The field \"{0}\" does not match the regular expression \"{1}\".", this.GetFieldDisplayName(), parameter);
 return this.GetFailedResult(ValidatorResult.Error);

So you should create a new validator class and use it in your validation rule by passing the class and assembly name in the Type field. Here is an example of OOTB validator that is checking the encoded character.

  Item obj = this.GetItem();
  if (obj == null || !obj.Paths.IsContentItem)
    return ValidatorResult.Valid;
  string name = obj.Name;
  string displayName = obj.DisplayName;
  if (this.ContainsEncodedAndDecodedSymbols(name))
  {
    this.Text = this.GetText(Translate.Text("The item name contains both encoded characters and characters that are used to replace encoded characters when Sitecore generates an item URL."));
    return this.GetFailedResult(ValidatorResult.Error);
  }
  if (!this.ContainsEncodedAndDecodedSymbols(displayName))
    return ValidatorResult.Valid;
  this.Text = this.GetText(Translate.Text("The item display name contains both encoded characters and characters that are used to replace encoded characters when Sitecore generates an item URL."));
  return this.GetFailedResult(ValidatorResult.Error);  

So you need to use the code that is given in the RegexValidator class and pass the name of the item, not the field.

And it will solve your issue.

1
  • Thank you Sumit for the idea. For me, the config change worked and i dont have to use code.
    – Yeshwanth
    Commented Feb 22, 2023 at 10:15
1

You can tell Sitecore how is your regex pattern through a setting.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <settings>    
            <setting name="ItemNameValidation">
                <patch:attribute name="value">^.*$</patch:attribute>
            </setting>
        </settings>
    </sitecore>
</configuration>

Just replace the pattern ^.*$ by yours.

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.