Search code examples
javascriptpdfadobelivecycle-designer

How can I prevent duplicate validation messages in Adobe Livecycle using JavaScript


I have a PDF containing multiple tables of data, each with multiple rows. It is a Dynamic XFA-data PDF. Validation works fairly well, as long as it is on one field only, but now I have a more complex validation. If one field is populated, another field has to be populated, and vice versa. I added validation JavaScript to the Foo field, and similarly to the Bar field. The problem is that the validation messages are popping up multiple times for the same field, when I tab out of the field with an invalid value. I have tried setting the xfa.event.rc to true/false (depending on whether the field is valid or not). This is being used successfully in other fields on the PDF, but it seems to have no effect, and I can't find any documentation on xfa.event.rc, so it may be deprecated. I think it used to be the return code. I am using EC4 now, but this PDF was originally scripted for ES3. Here is the JavaScript for the "Foo" validate event. Selecting to display all the messages in the same box does not correct the problem. It displays the same message multiple times. I know this, because I added a line number variable to the message.

MyXmlRoot.FirstItemsList.ItemDataRow.Foo::validate - JavaScript, client)
this.validationMessage = "";
if (!this.isNull && this.rawValue != ""){
    var bar= xfa.resolveNode("MyXmlRoot.FirstItemsList.ItemDataRow.Bar");
    if (bar.isNull || bar.rawValue == ""){
       this.validationMessage = "A bar is required if a foo has been entered.";                 
    } 
}

Solution

  • I tried this approach and it worked for me:

    Bar field validate event:

    MyXmlRoot.FirstItemsList.ItemDataRow.Bar::validate - (JavaScript, client)
    script.validateFooBar(Foo, this);
    

    Foo field validate event:

    MyXmlRoot.FirstItemsList.ItemDataRow.Foo::validate - (JavaScript, client)
    this.validationMessage = "A bar is required if a foo has been entered.";
    script.validateFooBar(this, Bar);
    

    script scriptobject:

    MyXmlRoot.FirstItemsList.ItemDataRow.#variables[0].script - (JavaScript, client)
    function validateFooBar(foo, bar){
        return !(!foo.isNull && bar.isNull);       
    }
    

    And here a picture of my hierarchy to make it clearer: enter image description here
    Now I only get one alert when

    1. I delete the value of bar while foo is filled
    2. I fill the value of foo while bar is empty