2

I'm currently implementing a custom validation logic, and I'm planning to leverage the existing fields of the Validation Rule template such as the Text field to be used as an error message. Is this possible? if yes how do you get the values?

enter image description here

2 Answers 2

1

If you will see the implementation of the method called this.GetText(), that you are using under your custom validation logic, it looks like this.

protected virtual string GetText(string text, params string[] arguments)
{
  Assert.ArgumentNotNull((object) text, nameof (text));
  Assert.ArgumentNotNull((object) arguments, nameof (arguments));
  string str = string.Empty;
  Database database = Context.Database;
  if (database == null && this.ItemUri != (ItemUri) null)
    database = Factory.GetDatabase(this.ItemUri.DatabaseName, false);
  Item obj = database == null ? (Item) null : database.GetItem(this.ValidatorID);
  if (obj != null)
    str = obj[StandardValidator.textFieldId];
  if (string.IsNullOrEmpty(str))
    str = this.Parameters["Text"];
  if (!string.IsNullOrEmpty(str))
    text = str;
  return Translate.Text(text, (object[]) arguments);
}

So it first checks for the ValidatorID variable using this.

Item obj = database == null ? (Item) null : database.GetItem(this.ValidatorID)

If it gets the Item, then it checks for the textFieldId and i.e. this

private static readonly ID textFieldId = new ID("{5DCF6FF3-41C5-466C-AD5E-991CFCD55716}");

If you see this field in the CMS, you will see that this is the ID for the field that you are looking for. Here is the screenshot.

enter image description here

And if this field is blank then it does the further operations.

So I think you need to pass the value in this.ValidatorID variable and it should get the value of the text field that you want to use.

Hope this makes sense.

9
  • This doesn't make sense to me. Does this get the value of the Text field? for example in this item /sitecore/system/Settings/Validation Rules/Field Rules/Common/Is Integer? Tried running the code but I got error on this line: str = obj[StandardValidator.textFieldId]; it says StandardValidator doesnt have definition for textFieldId.
    – Borj
    Commented Oct 17, 2022 at 14:25
  • Did you check that what item you are getting in obj and does it contain the field you are looking for? Commented Oct 17, 2022 at 14:30
  • Database points to core, while obj is null
    – Borj
    Commented Oct 17, 2022 at 15:14
  • what you are passing in this.ValidatorID? Commented Oct 17, 2022 at 15:19
  • {D3BCF474-92AB-4BF4-8508-5D6881F0ED0E} which is the guid of my custom validation rule.
    – Borj
    Commented Oct 17, 2022 at 15:48
0

You can simply use the Text property from the BaseValidator class to get or set the validation text. (You'll have to make your custom validator class inherit the StandardValidator)

Here's a sample code. I hope it'll help :)

    public class MyCustomValidator : StandardValidator
    {
        public override string Name => "My Custom Validator";


        protected override ValidatorResult Evaluate()
        {
            try
            {
                //do something
                if(valid)
                    return ValidatorResult.Valid;
                else
                {
                    //**Here you can directly get or set the validation text like this**
                    var text = Text;
                    Text = $"Oops! Your item has validation errors.{text}";
                    return GetMaxValidatorResult();
                }
            }
            catch
            {
                //do something
                return ValidatorResult.Unknown;
            }

        }

        protected override ValidatorResult GetMaxValidatorResult()
        {
            return GetFailedResult(ValidatorResult.Error);
        }
    }

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.