1

Is there any way to validate regex pattern input on the rule editor when the string operator condition is selected as match regular expressions? We would like to avoid typos on regex input.

enter image description here

2
  • Can you please add some more details? Apr 18 at 7:19
  • For example, I added condition to rule where comparison is selected as matches the regular expression . Based on this condition, I would like to validate specific value entered on Enter Value prompt dialog box on Rule Editor
    – rkrathna
    Apr 18 at 19:48

1 Answer 1

1

After investigating it on my local instance I found that this is not possible OOTB. However, when I tried to customize it I found where the functionality is given. Here is what I found.

  1. When I searched for the textbox taking the input on the rule editor window, I found that it is coming from a file called Prompt.xml and the functionality is being handled using a JS file called prompt.js that exists on the below path.

    \wwwroot\<your-site-root>\sitecore\shell\Applications\Dialogs\Prompt

  2. I added a new function in the prompt.js file to validate the regex pattern. Here is the code.

    function isValidRegex(s) {
       try {
           const m = s.match(/^([/~@;%#'])(.*?)\1([gimsuy]*)$/);
           return m ? !!new RegExp(m[2],m[3]) : false;
       } 
       catch (e) {
           return false
       }
    } 
    
  3. In this file I called this function in the ok_click event like below.

     function ok_click(evt) 
     {
         var inputValue = $("Value").value;
    
         if(!isValidRegex(inputValue))
         {
            alert("not a valid regex");
            return;
         }
         ....
         ....
         ....
      }
    

So now if you click on the OK button it will validate and show you a message that the regex is not valid.

But I analyzed that this is not the place where it should be customized. Because it will work for all types of rules as I didn't find a way to identify it with a particular rule to validate the regex.

After investigating it more, I observed that it should be validated after all the rules are set by the user. When the user clicks on the OK button on the Create Rule window it is handled by this function that exists in the Sitecore.Client DLL under class Sitecore.Shell.Applications.Rules.RulesEditor.RulesEditorPage. Here is the source code.

protected override void OK_Click()
{
  string rulesXml = this.Rules;
  if (string.IsNullOrEmpty(rulesXml))
    rulesXml = "-";
  else if (new RulesDefinition(rulesXml).GetValidationErrors().Count > 0)
    SheerResponse.Alert("This rule may not function correctly if you do not enter values for all the parameters in all the conditions and actions.");
  SheerResponse.SetDialogValue(rulesXml);
  base.OK_Click();
}

Finally, on the GetValidationErrors() function you will get all the rules added by the user and it should be customized here.

Now the answer to the question is you need to extend this more to handle your situation and then use this with your solution.

Hope this help you to understand what you are looking for.

1
  • Thank you @sumit-bhatia I was able to override OK_Click to customize validation for rule editor. I appericate your effort. Also we can use ~/sitecore/shell/override folder to place custom xml files and source control instead of replacing OOTB. Out of the box Sitecore won't look for overrided xml files under this folder. so we have to include patch for xamlSharp.
    – rkrathna
    Apr 24 at 17:02

Your Answer

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

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