Setting a required field with client code

Hello community!

I need to set fields required based in values of a lookup at client side.

I was trying to test how isRequired works in attributes , but I found that the only way to work with it, is declaring the explicit value to True or False.

Tried to use a virtual column, and a method function, but it behaves weird.  

Here are my examples:

and this how it looks on screen:

 

It's showing the "Enter a Value" warning but they are supposed not to be required (* is missing).

 

Is there any way to workaround with this?

 

Thank you for your help.

Like 0

Like

3 comments
Best reply

Cesar Jaramillo,

Let's see what method of making fields required* dynamically actually works good – it is using rules/businessRules.

You can create a rule for your field, the condition can be any value of the page and its related lookups.

Like here:

But if you have to do this for several fields, it will take much time to set them for each field. 

And if there are many conditions or they are too complex, "businessRules" abilities can be not enough.

But we can set an attribute (boolean) to be a condition for each of these rules, like here:



The attribute value can be changed dynamically using "methods".

 

So here's what i'd advise you to do:



In my example the fields "Email" and "Preferred language" become required, if the contact Type is "Customer" and the "Owner" field is not empty.

1. Add an attribute of type BOOLEAN

attributes: {
	"FullInfoRequired": {
		"dataValueType": Terrasoft.DataValueType.BOOLEAN,
		"dependencies": [
			{
				"columns": ["Owner", "Type"],
				"methodName": "setFullInfoRequired"
			}
		]
	}
},

Each time any of the columns ("Owner" or "Type") is changed, the method "setFullInfoRequired" is called.

2. Add the method

setFullInfoRequired: function() {
	var type = this.get("Type");
	// ContactType.Client is a GUID for "Customer" type
	if (type.value === confConsts.ContactType.Client && this.get("Owner")) {
		this.set("FullInfoRequired", true);
	}
	else this.set("FullInfoRequired", false);
}

To set the attribute on page loading, immediately after the page loaded its data, you should also call this function in "onEntityInitialized":

onEntityInitialized: function() {
	this.callParent(arguments);
	this.setFullInfoRequired();
}

3. Add the rules using Page designer, as in my 2nd screenshot, for each field that should become required.

 

If the conditions are simple, just use businessRules as in the 1st screenshot.

 

Hello Cesar,

 

The isRequired attribute that uses the value that method returns works correctly in my local app (using bindTo: {"method name here"}). It seems that there is either some other custom logic related to these columns in the ContactPageV2 or maybe custom CSS that hides these * symbol. You need to look through all the logic related to these columns where you need to have the * symbol.

 

Best regards,

Oscar

Oscar Dylan,

Hi Oscar,  In my case, when the method returns True it works fine, but when it returns False, the * is gone, but the warning remains and it doesn't allow to save.  In what version did you try it?

Thanks!

Cesar Jaramillo,

Let's see what method of making fields required* dynamically actually works good – it is using rules/businessRules.

You can create a rule for your field, the condition can be any value of the page and its related lookups.

Like here:

But if you have to do this for several fields, it will take much time to set them for each field. 

And if there are many conditions or they are too complex, "businessRules" abilities can be not enough.

But we can set an attribute (boolean) to be a condition for each of these rules, like here:



The attribute value can be changed dynamically using "methods".

 

So here's what i'd advise you to do:



In my example the fields "Email" and "Preferred language" become required, if the contact Type is "Customer" and the "Owner" field is not empty.

1. Add an attribute of type BOOLEAN

attributes: {
	"FullInfoRequired": {
		"dataValueType": Terrasoft.DataValueType.BOOLEAN,
		"dependencies": [
			{
				"columns": ["Owner", "Type"],
				"methodName": "setFullInfoRequired"
			}
		]
	}
},

Each time any of the columns ("Owner" or "Type") is changed, the method "setFullInfoRequired" is called.

2. Add the method

setFullInfoRequired: function() {
	var type = this.get("Type");
	// ContactType.Client is a GUID for "Customer" type
	if (type.value === confConsts.ContactType.Client && this.get("Owner")) {
		this.set("FullInfoRequired", true);
	}
	else this.set("FullInfoRequired", false);
}

To set the attribute on page loading, immediately after the page loaded its data, you should also call this function in "onEntityInitialized":

onEntityInitialized: function() {
	this.callParent(arguments);
	this.setFullInfoRequired();
}

3. Add the rules using Page designer, as in my 2nd screenshot, for each field that should become required.

 

If the conditions are simple, just use businessRules as in the 1st screenshot.

 

Show all comments