Dynamic Parameter Validator in Freedom UI

Hi Community.

Here example validator client side in Documentary

/* Declare the AMD module. */

define("UsrAppValidators_FormPage", /**SCHEMA_DEPS*/[]/**SCHEMA_DEPS*/, function/**SCHEMA_ARGS*/()/**SCHEMA_ARGS*/ {

    return {

        viewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[

            {

                "operation": "insert",

                "name": "UsrName",

                "values": {

                    "layoutConfig": {

                        "column": 1,

                        "row": 1,

                        "colSpan": 1,

                        "rowSpan": 1

                    },

                    "type": "crt.Input",

                    "label": "$Resources.Strings.UsrName",

                    "control": "$UsrName"

                },

                "parentName": "LeftAreaProfileContainer",

                "propertyName": "items"

            }

        ]/**SCHEMA_VIEW_CONFIG_DIFF*/,

        viewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG*/{

            "attributes": {

                "UsrName": {

                    "modelConfigDiff": {

                        "path": "PDS.UsrName"

                    },

                    "validators": {

                        /* Bind the custom validator to the attribute. */

                        "MyValidator": {

                            "type": "usr.MyValidator",

                            "params": {

                                "invalidName": "test",

                                "message": "Invalid name"

                            }

                        }

                    }

                },

                "Id": {

                    "modelConfigDiff": {

                        "path": "PDS.Id"

                    }

                }

            }

        }/**SCHEMA_VIEW_MODEL_CONFIG*/,

        modelConfigDiff: /**SCHEMA_MODEL_CONFIG*/{

            "dataSources": {

                "PDS": {

                    "type": "crt.EntityDataSource",

                    "config": {

                        "entitySchemaName": "UsrAppValidators"

                    }

                }

            }

        }/**SCHEMA_MODEL_CONFIG*/,

        handlers: /**SCHEMA_HANDLERS*/[]/**SCHEMA_HANDLERS*/,

        converters: /**SCHEMA_CONVERTERS*/{}/**SCHEMA_CONVERTERS*/,

        validators: /**SCHEMA_VALIDATORS*/{

            /* The validator type must contain a vendor prefix.

            Format the validator type in PascalCase. */

            "usr.MyValidator": {

                "validator": function (config) {

                    return function (control) {

                        return control.value !== config.invalidName ? null: {

                            "usr.MyValidator": { message: config.message }

                        };

                    };

                },

                "params": [

                    {

                        "name": "invalidName"

                    },

                    {

                        "name": "message"

                    }

                ],

                "async": false

            }

        }/**SCHEMA_VALIDATORS*/

    };

});



is it possible to give argument of validator (invalidName) dynamic from other field value instead static like "invalidName": "test"? or can field value (other than control) accessed inside validator method?

Like 0

Like

3 comments

As far as I know, this is not possible at the moment to get a value of other attributes in a validator.

Alex Zaslavsky,

Thanks for your answer, really appreciate it.

I was told by support that it currently isn't possible, and their recommended workaround was to save the value to the JS global variable `window`, the example they gave was:

viewModelConfig: /**SCHEMA_VIEW_MODEL_CONFIG*/{
"attributes": {                
    "DateTimeAttribute_lpcf566": {
        "modelConfig": {
            "path": "PDS.StartDate"
        },
        "change": {
            "request": "dw.StartDateChange",
        },
        "validators": {
            "StartDateIsNotNotLessThanProjectStartDate": {
                "type": "dw.StartDateIsNotNotLessThanProjectStartDate",
                "params": {
                    "message": "#ResourceString(StartDateIsNotNotLessThanProjectStartDateMessage)#",
                }
            }
        }
    },
}
},/**SCHEMA_VIEW_MODEL_CONFIG*/
handlers: /**SCHEMA_HANDLERS*/[
{
    request: "dw.StartDateChange",
    handler: async (request, next) => {
        /********/window.StartDate = await request.$context.DateTimeAttribute_lpcf566;/********/
        if(request.$context.IsStartDateInited) {
            request.$context.validate();
        }
        request.$context.IsStartDateInited = true;
        return next?.handle(request);
    }
}
],/**SCHEMA_HANDLERS*/
validators: /**SCHEMA_VALIDATORS*/{
"dw.StartDateIsNotNotLessThanProjectStartDate": {
    "validator": function (config) {
        return function (control) {
            return (!control.value || control.value >= /********/window.StartDate/********/)
                ? null
                : {"dw.StartDateValidator": {message: config.message}};
        };
    },
    "params": [
        {
            "name": "message"
        }
    ],
    "async": false
}
}/**SCHEMA_VALIDATORS*/

 

Where I've highlighted with surrounding comments the usage of the window to store this additional information to be used by the validator. Definitely not ideal, and hopefully they'll add this functionality in future, since async validators to perform some checks against the record when some field changes (and so where the validator will need the record's ID) seem to be the most useful usage of them, but this should at least work in the meantime.

Show all comments