Visual Builder

Get Involved. Join the Conversation.

Topic

    Stephen Bryant
    regExp in Visual BuilderAnswered
    Topic posted June 7, 2019 by Stephen BryantBronze Trophy: 5,000+ Points, last edited June 7, 2019, tagged Business Objects, Groovy, UI, Validation 
    89 Views, 12 Comments
    Title:
    regExp in Visual Builder
    Summary:
    Attempting to Implement Pattern Match
    Content:

    Hello,

    I need some guidance on how to best go about validating a field for a particular pattern.

    I've tried implementing the Jet Cookbook approach but cannot import the RegExpValidatorFactory.  I wanted to use the validator property on the field and I figured this was the way to go about it.  

    I also checked out creating a regExp validator on the business object but the compile method is not allowed on the server.

    The pattern I am implementing is simple but how to get it into Visual Builder is the problem.  If someone can point me in the right direction (e.g., how to import the regExp validation factory) that would be great.

    Thanks,

    Steve

    **Update**

    I imported the validators this way and it worked --- "define(['ojs/Validator','ojs/RegExpValidator'], function(oj) { ...}

    I created a blur event on the field calling this function:

     PageModule.prototype.plCodeValidator = function(plCode) {

        var rvf = oj.Validation.validatorFactory(oj.ValidatorFactory.VALIDATOR_TYPE_REGEXP);
        var plCodeValid = rvf.createValidator({
          'pattern': '\d{3}\-\d{3}',
          'messageDetail': 'You must enter two sets of three numbers separated by a dash'     
          
        });
        
        if(plCodeValid.validate(plCode)) {
          
          return true;
          
        } else {
          
          return false;
        }

      };

    I am now getting the following errors in Chrome Dev Tools:

    CallModuleAction (callModuleFunction1_cakqkh3et) The module does not exist or the function 'plCodeValidator' does not exist in the module.

    Action CallModuleAction failed. CallModuleAction (callModuleFunction1_cakqkh3et) The module does not exist or the function 'plCodeValidator' does not exist in the module.

     

    Can anyone help?  It appears the validators will not import.  I can verify that in devtools.  Am I barking up the wrong tree?

    Thanks,

    Steve

     

     

     

     

     

    Version:
    19.1.3

    Best Comment

    Shay Shmeltzer

    does it work if you use another / so make it - "pattern": "//d{3}-//d{3}", 

    Also - what would be a valid value in the field for you?

    Comment

     

    • Stephen Bryant

      Even a simpler function like below is throwing the same errors.  It works in sublime so I am at a loss.

       PageModule.prototype.plValidator = function(str) {
          
        var res = str.match(/\d{3}\-\d{3}/);
        
        if(res) {
          
          return true;
          
        } else {
          
          return false;
          
        }
        
        
        };  

    • Stephen Bryant

      No matter what I do, VBCS will not recognize that I have imported the library.  The javascript library I imported is clearly in the directory/path I am indicating and the function indicated that when I created it.  However, using blur event on the field and calling the function yields the same errors, "The module does not exist or the function 'plValidator' does not exist in the module."  That simply is not the case so I am at a loss here.

      Does anyone know what the issue could be?

      Thanks,

      Steve

      • JT Thomas

        Hi Stephen,

        If I understand you correctly, you imported these two Javascript libraries? Where did you place them, under resources/ojs? Does the auditor catch any errors or warnings in that code? I'd like to try to import them to reproduce. Can you provide the download link for them?

        Thanks,

        ~/jt

        • Stephen Bryant

          JT,

          Here is the link:

          https://github.com/VerbalExpressions/JSVerbalExpressions/tree/master/dist

          This is the function I created for the blur event:

          PageModule.prototype.plValidator = function(plCode) {
              
              const tester = myregexp.VerEx()
              .pattern('\d{3}\-\d{3}');    
              
              if (tester.test(plCode)) {
                
                return true;
                
              } else {
                
                return false;
                
              };    
              
            }; 

          The code auditor was fine and indicated no errors.  It all looks good until I use it in the UI.  

          Thanks,

          Steve

          • Stephen Bryant

            JT,

            I switched gears and found the verbalexpressions library out on github.

            Should I stay with the validatorfactory?

            Steve

    • Shay Shmeltzer

      I can put code like this in my VB HTML page directly:

      <oj-input-text id="oj-input-text-1957787903-3" class="oj-flex-item oj-sm-12 oj-md-4"
           validators='[{
                        "type": "regExp", 
                        "options" : {
                          "pattern": "(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,}", 
                          "label": "Password", 
                          "messageSummary" : "{label} too Weak",
                          "messageDetail": "You must enter a password that meets our minimum security requirements."}}]'></oj-input-text>

      Does this work for you?

      • Stephen Bryant

        Shay,

        I tried your code directly on the HTML but it did not work meaning it was like the code was not there.

        Not sure what is happening.

        Steve

      • Stephen Bryant

        Shay,

        I included your code correctly this time but the field will not display in the UI.

        Steve

      • Stephen Bryant

        Shay,

        Okay, your code produces a result for me but I think my pattern is not working.\

        Here is what I have for the pattern: \d{3}\-\d{3}

        The problem is that reverse slashes cause the field to not display in the UI so I removed them to create this pattern (d{3}-d{3})  but it does not work.

        How can I include the reverse slashes?

        Steve

    • Stephen Bryant

      Shay,

      Okay, it works but with this pattern: \\d{3}\\-\\d{3}.  The trick is to always update directly in the HTML and never on the UI property itself.  I did that and relaunched the screen and all is good.

      Thanks again,

      Steve