Expand my Community achievements bar.

SOLVED

AEM form required field check (Examples of Common Scripting Tasks)

Avatar

Level 3

I am trying to implement the mandatory field test using 'mandatory' or 'nullTest' properties by following the example in the official site here

 

I have a textfield, and a button. On the button click event I have the below code

 TextField1.mandatory = "error"  
 TextField1.mandatoryMessage = "this field is mandatory!"

The expected behavior is to highlight the text field if it is empty and display the mandatory message. However, I've encountered an issue where, on the first button click, the message is not immediately visible. It only becomes visible on the second click.

 

I am using AEM Form v6.5 (128 bit), and I'm wondering if this behavior is related to specific settings. Could someone kindly advise on why the mandatory message might not be visible on the first click? Your assistance is greatly appreciated.

 

Attached the PDF form for your reference.

 

Thanks.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 3

0,.

 

There are just some things you learn over time. layout.relayout() is not well documented if documented at all.

The code you wrote on the click event sets the text field to required or mandatory. But the form did not really know that because you are still running the second line of code. So to the form the second line of code finds that the text field is not set to mandatory. You have to basically lose focus on the button and then run the code again with the second click for the form to have caught up enough to know that the text field is now mandatory.

 

So the second time you click the button it sets the text field to mandatory but it is already mandatory so the second line finds that the text field is mandatory so it fires the message.

 

layout.relayout() basically forces the form to look at itself and get a report on what the status of all fields are. This is not a technical report, I am anthropomorphizing the form so that it seems like a sentient being. It just helps me to understand what is going on with the form. So when layout.relayout() is run the form knows that the text field is mandatory so the second (now third) line of code will see the field as mandatory and throw the message.

View solution in original post

3 Replies

Avatar

Level 3

0,

 

If you add the following between your two lines of code it will work on first click:

xfa.layout.relayout();

 

Regards,

Avatar

Level 3

Hi @koolForms , Thanks

But, why I need to write the lice of code you gave? Any explanations?

Avatar

Correct answer by
Level 3

0,.

 

There are just some things you learn over time. layout.relayout() is not well documented if documented at all.

The code you wrote on the click event sets the text field to required or mandatory. But the form did not really know that because you are still running the second line of code. So to the form the second line of code finds that the text field is not set to mandatory. You have to basically lose focus on the button and then run the code again with the second click for the form to have caught up enough to know that the text field is now mandatory.

 

So the second time you click the button it sets the text field to mandatory but it is already mandatory so the second line finds that the text field is mandatory so it fires the message.

 

layout.relayout() basically forces the form to look at itself and get a report on what the status of all fields are. This is not a technical report, I am anthropomorphizing the form so that it seems like a sentient being. It just helps me to understand what is going on with the form. So when layout.relayout() is run the form knows that the text field is mandatory so the second (now third) line of code will see the field as mandatory and throw the message.