SCA Kilimanjaro - how do we extend validation patterns

19 Views Asked by At

We use SuiteCommerce Advanced Kilimanjaro and I have a question regarding being able to extend the Backbone.Validation.patterns fields. Ultimately I would like to call a new pattern we're creating from within code as such; Backbone.Validation.patterns.customPattern.test().

In the third_parties directory there is the backbone-validation module that includes backbone-validation.js, which is where the base logic is for validation. In the suitecommerce/BackboneExtras module there's Backbone.Validation.patterns.js, which looks like it's an extension to the base .patterns object:

define('Backbone.Validation.patterns'
,   [   'Backbone'
    ,   'underscore'
    ,   'Backbone.Validation'
    ]
,   function (
        Backbone
    ,   _
    )
{
    'use strict';

    _.extend(Backbone.Validation.patterns,
    {
        // Same as email but is more restrictive and matches the same emails as the Netsuite backend UI
        // Source: https://system.netsuite.com/javascript/NLUtil.jsp__NS_VER=2014.1.0&minver=154&locale=en_US.nlqs
      //    (Search for NLValidationUtil_SIMPLE_EMAIL_PATTERN)
        email: /^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[a-z0-9]+(?:-+[a-z0-9]+)*\.)+(?:xn--[a-z0-9]+|[a-z]{2,16})$/i

        //This validation is less restrictive than standard and matches with the used in Netsuite backend
    ,   netsuiteUrl: /^(https|http|ftp|file):\/\//

    ,   netsuiteFloat: /^-{0,1}([0-9])+(\.{1}[0-9]+)?$/

    ,   netsuiteInteger: /^-{0,1}([0-9])+$/
        //Allow numbers bettwen 000.00 and 000100.00 ending with optional %
    ,   netsuitePercent: /^0*((([0-9]{1,2})(\.[0-9]{1,2})?%?$)|(100(\.0{1,2})?%?$))/

        //Allow any character 7 or more times (this is the validation that the netsuite backend form does on phone type fields)
    ,   netsuitePhone: /^.{7,}$/

    });

    return Backbone.Validation.patterns;
});

Now, I want to add a new validator pattern so I thought I would simply create a new module and extend the same class, but it isn't working as my custom pattern isn't being added to the Backbone.Validation.patterns object. I created my extension module, extensions/BackboneExtras.Ext/Backbone.Validation.patterns.Ext.js and added the following code (the regex is strictly for testing, it's not what's intended so don't focus on that):

define('Backbone.Validation.patterns.Ext'
,   [   'Backbone'
    ,   'underscore'
    ,   'Backbone.Validation'
    ]
,   function (
        Backbone
    ,   _
    )
{
    'use strict';

    _.extend(Backbone.Validation.patterns,
    {
        customPattern: /^[A-Ba-b0-9]+$/i
    });

    return Backbone.Validation.patterns.Ext;
});

This doesn't work. If I console out Backbone.Validation.Patterns my custom pattern isn't being added. I've also tried adding a return to Backbone.Validation.patterns itself. I've verified that ns.package.json file in the module is correct and that my module is correctly being added to the application in distro.json. I know that this is rookie-level stuff, but I'm not sure what I'm doing wrong here.

Thanks for any and all help!

0

There are 0 best solutions below