How to perform cross-field date validation (e.g., Detection Date vs Birth Date) using custom Validators in Freedom UI?
Hi Community,
We are working on a Freedom UI page where we need to implement cross-field validation on a date field. Specifically, we want to validate that the Detection Date (FechaDeteccion) is not earlier than the Child's Birth Date (FechaNacimiento).
In Classic UI, we used to handle this easily by accessing other model attributes within the validation method. However, in Freedom UI using custom validators declared in validators schema block, the validator function only receives the control instance (Angular AbstractControl), and properties like control.root or control.parent do not give us access to other Page ViewModel attributes/context.
We are currently passing $context via viewModelConfigDiff parameters like this:
JS
"PDS_HcoFechaDeteccionSospecha_s35z4sf": {
"modelConfig": {
"path": "PDS.HcoFechaDeteccionSospecha"
},
"validators": {
"ValidarSospechaVsNacimiento": {
"type": "usr.HcoFechaSospechaNacimientoValidator",
"params": {
"message": "La fecha no puede ser anterior a la fecha de nacimiento.",
"campoNacimiento": "PDS_HcoFechaNacimiento_04pdwdb",
"context": "$context"
}
}
}
}
And in the validator declaration:
"usr.HcoFechaSospechaNacimientoValidator": {
"validator": function(config) {
return function(control) {
let fecha = control.value;
if (!fecha) return null;
let ctx = config.context;
// Reading secondary field from passed context...
let fechaNacVal = ctx ? ctx[config.campoNacimiento] : null;
// Date comparisons...
return isInvalid ? { "usr.HcoFechaSospechaNacimientoValidator": { message: config.message } } : null;
};
},
"params": [
{ "name": "message" },
{ "name": "campoNacimiento" },
{ "name": "context" }
],
"async": false
}
Our Questions for the Community:
- Is passing
"context": "$context"inviewModelConfigDiffthe recommended OOTB standard in Freedom UI for cross-field validation, or is there a native SDK approach / API method to inspect other attributes from within a custom validator? - Alternatively, is there a recommended way to trigger/update the validation status of
FechaDeteccionprogrammatically from acrt.HandleViewModelAttributeChangeRequesthandler without relying onalert()or manual field resetting?
Any insights or best practices on how you are handling multi-field dynamic validations in Freedom UI would be greatly appreciated!
Thanks in advance!
Like