How to configure field validation so that a user cannot enter a number smaller than the value in another field on the page?

"PDS_UsrActualFuelRemaining_s06444m": {
                        "modelConfig": {
                            "path": "PDS.UsrActualFuelRemaining"
                        },
                        "validators": {
                            "MyNumberValidator": {
                                "type": "usr.MonthNumberValidator",
                                "params": {
                                    "minValue": "0",
                                    "message": "Допустимі значення від 0 до 500",
                                    "maxValue": "500"
                                }
                            }
                        }
                    },

 

If I specify a range, the validator works. But when I try to use <strong>var OldWorkHour = await request.$context.PDS_UsrWorkHours_sjx2srh;</strong>, I get an error.

Could you please help me figure out why this happens and how to fix it?

 

 "usr.MonthNumberValidator": {
   validator: function(config) {
       return function(control) {
           var controlValue = control.value !== undefined ? parseFloat(control.value) : null; 
 
           var minValue = config.minValue !== undefined ? parseFloat(config.minValue) : Number.MIN_SAFE_INTEGER; 
           var maxValue = config.maxValue !== undefined ? parseFloat(config.maxValue) : Number.MAX_SAFE_INTEGER; 
 
            var OldWorkHour = await request.$context.PDS_UsrWorkHours_sjx2srh;
 
           var isValueValid = controlValue >= minValue && controlValue <= maxValue;
           if (!isValueValid) {
 
               return {
                   "invalid": { 
                       message: config.message || "Введене значення не відповідає умовам"
                   }
               };
           }
 
           // Якщо все добре, повертаємо null (успішна валідація)
           return null;
       };
   },
   params: [
       {
           name: "minValue"
       },
       {
           name: "maxValue"
       },
       {
           name: "message"
       }
   ],
   async: false
},
Like 0

Like

2 comments

Hi,

 

And what is the datatype for the PDS_UsrWorkHours_sjx2srh and UsrActualFuelRemaining? And what is the error message you receive?

Hello,
The ability to add dynamic parameters to a validator is still under development, as a way around you can try using global variables like window. For example

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);
    }
}]                
...        

"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
}

Show all comments