Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Standard Message Box does not appear when I script a field to be mandatory

Avatar

Level 2

I am trying to create some code (Javascript) to say if the Contact #  is Null then make the 2 fields below mandatory.  This code works, the only problem is that it does not put up the Standard message "First name: cannot be left Blank that I would get if I just did this in the Designer tool itself. 

When I run the form, I make sure that the Contact # is empty and then I make sure either the First Name (Favor) or Last Name (Fanam) is empty.  Based on which one is empty it should say "????" cannot be left blank.  If both are empty then it can just put up the message for the first one. If I click around enough on the form then eventually it appears, but not right away.  I have this code in a Button Click.  Does anyone know of a way I can get the standard message to appear right away if the Contact # is null?  If I do this in the designer tool itself (i.e., not scripting) then it works, but I need to do this via scripting because it has to evaluate if Contact # is null first.

 

var lv_contactno = xfa.resolveNode("xfa.form.data.MainSubform.Contact02Subform.CNTCTNODRPDWN2").rawValue;

if (lv_contactno != null)

{

 

xfa.resolveNode("data.MainSubform.Contact02Subform.FAVOR").mandatory = "error";

xfa.resolveNode("data.MainSubform.Contact02Subform.FANAM").mandatory = "error";

}

2 Replies

Avatar

Level 9

Put the script in the exit event of the ContactNo. So when the contact not empty it will make both the Name fields as mandatory.

Then in the exit event of both FirstName, Last name check which Name field has value, according to that make the other one mandatory and show message.

In the contact no exit event put the script

var lv_contactno = xfa.resolveNode("xfa.form.data.MainSubform.Contact02Subform.CNTCTNODR PDWN2").rawValue;
if (lv_contactno != null)
{
  xfa.resolveNode("data.MainSubform.Contact02Subform.FAVOR").mandatory = "error";
  xfa.resolveNode("data.MainSubform.Contact02Subform.FANAM").mandatory = "error";

}

In the exit event of this FirstName put the script

if (this.rawValue == null)

     {

          if (FNAM.rawValue == null) // If both the values are null then prompt the use to fill either one.

               {

                    app.alert("Please fill either the firstname or the last name");

               }

          else

               {

                     FAVOR.mandatory = "disabled"; // Make the FirstName as optional

               }

     }

else

     {

          FNAM.mandatory = "disabled"; // Make the lastname field as optional

     }

Similarly in the exit event of this LastName put the script

if (this.rawValue == null)

     {

          if (FAVOR.rawValue == null) // If both the values are null then prompt the use to fill either one.

               {

                    app.alert("Please fill either the firstname or the last name");

               }

          else

               {

                     FNAM.mandatory = "disabled"; // Make the LastName as optional

               }

     }

else

     {

          FAVOR.mandatory = "disabled"; // Make the firstname field as optional

     }

Hope this helps.

Thanks,

Bibhu.

Avatar

Level 2

Thx for the Reply Bibhu,

Your examples are helpful, but unfortunately my scenario gets a lot more complicated.  I had just posted a small section of it.  I was hoping to not have to do a seperate message box or app.alert.  It is looking like I will have to.  I do think I may be able to use some of your code example though for something else.  thx!