• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Validation Code Assistance

Explorer ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

I have some validation code (below) that validates fields before a user can submit the form. The code works great, with one exception; Each time it catches one of the below, the code returns to the form and designated field, as should; but also clears all fields on the the entire form?

Anyone have any ideas as to why, upon returning to the form and designated field, the form clears all data from all fields; almost as if it is resetting the form?

Here is what I am working with?

if (this.getField("Date of Loss").valueAsString=="")

{app.alert("You must enter the Accident Date in the Date of Loss field.");

getField("Date of Loss").setFocus();}

else if (this.getField("Time of Loss").valueAsString=="")

{app.alert("You must enter the Time the accident occurred in the Time of Loss field.");

getField("Time of Loss").setFocus();}

else if (this.getField("Employee Drivers Name").valueAsString=="")

{app.alert("You must enter the Employee Drivers Name in the Employee Drivers Name field.");

getField("Employee Drivers Name").setFocus();}

else

{// Submit form - sends a copy of the form...bla bla bla bla bla}

TOPICS
Acrobat SDK and JavaScript

Views

288

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 16, 2019 Apr 16, 2019

That would be nice if you could abort the action sequence, maybe if you forced an exception. But I've never run into this situation. I always put all my code into a single action. If there is a JavaScript, then that is usually all that is needed. Everything can be scripted.

If you really want to keep your current structure, then you'll need to set a blocking variable and then guard all the code with "if" statements.  Be sure to reset the blocking variable when appropriate. But this is a really co

...

Likes

Translate

Translate
Community Expert ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

There is obviously some other code running that is performing the reset. Find the other code.

What's at the bottom of the if block?

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

Hi Thom -

Thanks again for your response! Omg...duh...yes...you are correct...sorry bout that. Didn't even think about the rest of the stuff. Guess I figured this would/should stop the form from processing any further code when it hits one of these validations.

So, lol..new question...is there a way to stop processing the remaining code unless all of the above is met?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

I actually have quite a bit of code running after this particular code (in red) executes...

2019-04-16_16-18-34.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

That would be nice if you could abort the action sequence, maybe if you forced an exception. But I've never run into this situation. I always put all my code into a single action. If there is a JavaScript, then that is usually all that is needed. Everything can be scripted.

If you really want to keep your current structure, then you'll need to set a blocking variable and then guard all the code with "if" statements.  Be sure to reset the blocking variable when appropriate. But this is a really complicated strategy that's easy to mess up. I think you are better off putting everything into a single script, where you can control it all centrally.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

Thanks Thom. I was actually just thinking about that; combining everything into one script. I have been doing some research (even came across a couple of your articles from 2011 😉 ) and there does not seem to be an easy way of doing this.

I will try and re-structure and see if I can get the results I am looking for.

Thanks again Thom for your assistance. Will let you know if I come across anything exciting and/or my final result.

Best regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2019 Apr 17, 2019

Copy link to clipboard

Copied

LATEST

Hello -

In taking Thom's advise, I decided to make my first attempt at combining everything (see post #3) into one script...lol...and it actually worked! Thom's suggestion allowed me to stop at the validation rules before processing any further code.

Thank you again Thom for your suggestion!!!!

If anyone is curios or experiencing a similar scenario, here is my final working code; which I am sure can be streamlined in many ways, but it works 😉

//*********************Required Field Check***********************

if (this.getField("Date of Loss").valueAsString=="")

{app.alert("You must enter the Accident Date in the Date of Loss field.");

getField("Date of Loss").setFocus();

Return;}

else if (this.getField("Time of Loss").valueAsString=="")

{app.alert("You must enter the Time the accident occurred in the Time of Loss field.");

getField("Time of Loss").setFocus();

Return;}

else if (this.getField("Employee Drivers Name").valueAsString=="")

{app.alert("You must enter the Employee Drivers Name in the Employee Drivers Name field.");

getField("Employee Drivers Name").setFocus();

Return;}

else

//*********************Hide buttons***********************

getField("Attach").display = display.hidden;

getField("Reset").display = display.hidden;

getField("Submit").display = display.hidden;

//*********************Make All Fields Read Only***********************

for(var i = this.numFields - 1; i > -1; i--)

{var fieldName = this.getNthFieldName(i);

this.getField(fieldName).readonly = true;}

//*********************TimeStamp***********************

var f = this.getField ("Today2");

f.value = "Submitted - " + util.printd("mm/dd/yyyy h:MM tt", new Date());

//*********************Email Doc***********************

{

// To email adresss

var cToAddr = "email@email.com";

//var cToAddr = "email@email.com";

// CC email address

var cCCAddr = "email@email.com";

// Set the subject and body text for the email message

var cSubLine = "Vehicle Accident Report:  " + this.getField("Employee Drivers Name").value;

var cBody = "Attention Risk Management - \n\n" + "Attached you will find an Vehicle Accident Report for: " + this.getField("Employee Drivers Name").value + ".\n\n" + "The Accident took place on: " + this.getField("Date of Loss").value + "  at: " + this.getField("Time of Loss").value + ".\n\n" + "Please review the attached report and let me know should you have any questions or concerns.\n\n" + "Thank you"

// Send the entire PDF as a file attachment on an email

this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});

//*********************Show buttons***********************

getField("Attach").display = display.visible;

getField("Reset").display = display.visible;

getField("Submit").display = display.visible;

//*******************Unlock all fields********************

for(var i = this.numFields - 1; i > -1; i--)

{var fieldName = this.getNthFieldName(i);

this.getField(fieldName).readonly=false;};

//*******************Delete all attachments***************

this.dataObjects.map(function(a){return a.name;}).forEach(function(a){this.removeDataObject(a);});

//*******************Clear all fields********************

for(var i = this.numFields - 1; i > -1; i--)

{var fieldName = this.getNthFieldName(i);

this.getField(fieldName).value="";};

//*******************Set focus to 1st field********************

getField("Date of Loss").setFocus();

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines