How to Read Custom Attribute OR Other Field Value in Validator

Hi Team,

I need to validate lead closing date that should not be lesser than lead created date. For this I need to fetch value of 'request.$context.LeadDS_CreatedOn_gjir2pi' in validator directly or using some custom attribute. But I'm not able to fetch value or attribute in validator.

Also, I used global variable 'CreatedOnLeadDate' in LeadPage & populating this in handler method. But the issue is validator is executed before handler method, hence getting createdOn value as:
"Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)" that is wrong
 

Handler Method:

Handler Method

Validator Method:

console

Is there any solution to accomplish this need?

Thanks in Advance

 

 

Like 0

Like

2 comments
Best reply

Hi.

As an option, you can enable and disable validators using the enableAttributeValidator and disableAttributeValidator methods. For example I have two text columns on the page "INN" and "Code". Here is the handler that will enable the validator in case the INN and Code values are similar and the Code is filled in (I could also add the check if the innValue is filled in):

{
				request: "crt.HandleViewModelAttributeChangeRequest",
				handler: async (request, next) => {
					if (request.attributeName === 'INN' || request.attributeName === "Code") {
						const codeValue = await request.$context.Code;
						const innValue = await request.$context.INN;
						if (codeValue && codeValue === innValue) {
							request.$context.enableAttributeValidator('INN', "TaxpayerNumberValidator");
						} else {
							request.$context.disableAttributeValidator('INN', "TaxpayerNumberValidator");
						}
					}
					return next?.handle(request);
				}
			},

Here is the validator:

validators: /**SCHEMA_VALIDATORS*/{
			"usr.TaxpayerNumberValidator": {
				"validator": function (config) {
					return function (control) {
						return {
							"usr.TaxpayerNumberValidator": { message: config.message }
						};
					};
				},
				"params": [
					{
						"name": "invalidNumber"
					},
					{
						"name": "message"
					}
				],
				"async": false
			}
		}/**SCHEMA_VALIDATORS*/

and here is the validator added to the attribute:

"INN": {
						"validators": {
							"TaxpayerNumberValidator": {
								"type": "usr.TaxpayerNumberValidator",
								"params": {
									"invalidNumber": "123",
									"message": "Invalid number"
								}
							}
						}
					},

So I don't have any conditions in the validator, validator will always return the error message. But we control the availability of the validator in the HandleViewModelAttributeChangeRequest. And using this approach you can compare values and enable\disable the validator based on the result of the compare.

Hi.

As an option, you can enable and disable validators using the enableAttributeValidator and disableAttributeValidator methods. For example I have two text columns on the page "INN" and "Code". Here is the handler that will enable the validator in case the INN and Code values are similar and the Code is filled in (I could also add the check if the innValue is filled in):

{
				request: "crt.HandleViewModelAttributeChangeRequest",
				handler: async (request, next) => {
					if (request.attributeName === 'INN' || request.attributeName === "Code") {
						const codeValue = await request.$context.Code;
						const innValue = await request.$context.INN;
						if (codeValue && codeValue === innValue) {
							request.$context.enableAttributeValidator('INN', "TaxpayerNumberValidator");
						} else {
							request.$context.disableAttributeValidator('INN', "TaxpayerNumberValidator");
						}
					}
					return next?.handle(request);
				}
			},

Here is the validator:

validators: /**SCHEMA_VALIDATORS*/{
			"usr.TaxpayerNumberValidator": {
				"validator": function (config) {
					return function (control) {
						return {
							"usr.TaxpayerNumberValidator": { message: config.message }
						};
					};
				},
				"params": [
					{
						"name": "invalidNumber"
					},
					{
						"name": "message"
					}
				],
				"async": false
			}
		}/**SCHEMA_VALIDATORS*/

and here is the validator added to the attribute:

"INN": {
						"validators": {
							"TaxpayerNumberValidator": {
								"type": "usr.TaxpayerNumberValidator",
								"params": {
									"invalidNumber": "123",
									"message": "Invalid number"
								}
							}
						}
					},

So I don't have any conditions in the validator, validator will always return the error message. But we control the availability of the validator in the HandleViewModelAttributeChangeRequest. And using this approach you can compare values and enable\disable the validator based on the result of the compare.

Hi Oleg Drobina,

It's working now, Thanks for your support 👍

Show all comments