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

Submite form button - Javascript help

Community Beginner ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Hi,

I have a form that has required feilds. I also have a button that submits the form and excecutes a javascript to flatten the file as read only and then sends via email. 

I got this Script from a few posts and replies from try67 over the past years (Thank you try67).

 

for (var i = 0 ; i < this.numFields ; i++) {
var f = this.getField(this.getNthFieldName(i)) ;
if (f.type != "button") {
f.readonly = true ;
}
}

var targetEmail = "email@email.com";

var subjectLine = "Service Sheet" + " - " + this.getField("Engineer").valueAsString + " - " + this.getField("SRV Number").valueAsString;

var body = "Please find attached the service sheet for the SRV stated in the subject line above.";

this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});

 

This script works really well and does exaclty what I want it to do. However, it ignores the 'required' feilds.

Does anyone know how I can run the script only once all required feilds have been filled/selected?

Thank you

TOPICS
Edit and convert PDFs , How to , JavaScript , PDF forms

Views

849

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 , May 26, 2022 May 26, 2022

I think Bernd means you need to check them in the code, as it won't do that for you (unless you switch to submitForm instead of mailDoc, but then your fields will already be set as read-only...).

So the steps to take are:

- Validate that all the required fields are filled-in (meaning, their value is not the same as their default value)

- If not, show an error message and exit the script.

- If so, make the fields read-only and submit the file.

Votes

Translate

Translate
Community Expert ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Check the required fields before you execute this script.

Votes

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 Beginner ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Thank you for the response.

All of the feilds that are 'required' are already checked in the field properties. 

Pressing the 'send form' button that runs the script seems to ignore all required fields and will send a blank form as an email

Votes

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 ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

I think Bernd means you need to check them in the code, as it won't do that for you (unless you switch to submitForm instead of mailDoc, but then your fields will already be set as read-only...).

So the steps to take are:

- Validate that all the required fields are filled-in (meaning, their value is not the same as their default value)

- If not, show an error message and exit the script.

- If so, make the fields read-only and submit the file.

Votes

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 Beginner ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Ahh, I understand now.


Thank you very much. 


Sorry to ask, but do you have an example of this that I can tailor it to my own form? 

Votes

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 ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Yes, I posted code that does just that many times. Search the forum for "validateRequiredFields" and you should be able to find it.

Votes

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 Beginner ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Thank you.

I found the code based off your search terms. 

function validateRequiredFields(doc) {
var emptyFields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}

if (emptyFields.length>0) {
app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));
return false;
}
return true;
}

if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});

This code fixes the validation problem I had and solves point 1 & 2 from the instruction above

quote

So the steps to take are:

- Validate that all the required fields are filled-in (meaning, their value is not the same as their default value)

- If not, show an error message and exit the script.

- If so, make the fields read-only and submit the file.


By @try67


So how do I know combine the 2 working scripts so instead of finishing off the code with this.mailDoc({cTo: "me@server.com"}); I run the original script which flattens the file and includes a subject line etc..

The final part of the script i want to excecute after the validation is 

for (var i = 0 ; i < this.numFields ; i++) {
var f = this.getField(this.getNthFieldName(i)) ;
if (f.type != "button") {
f.readonly = true ;
}
}

var targetEmail = "email@email.com";

var subjectLine = "Service Sheet" + " - " + this.getField("Engineer").valueAsString + " - " + this.getField("SRV Number").valueAsString;

var body = "Please find attached the service sheet for the SRV stated in the subject line above.";

this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});

Votes

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 ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

Use this:

 

if (validateRequiredFields(this)) {

// your original code

}

Votes

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 Beginner ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

LATEST

You hero!!

Thank you so much

Votes

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