Copy link to clipboard
Copied
I have a form that will be submitted as a complete PDF. The form has required fields. Tried "Submit a Form" action but I cannot include specific fields from the form in the email subject.
I switched to a Javascript function to submit the form; now I get the specificity of the email I desire (with field answers included in the subject line) but the required fields no longer prevent the form from being sent if they are not filled.
Is there a way to get one method or the other to do both?
1. Not submit form via email unless required fields are filled
2. Place specific field answers in subject of submission email
mailto:email@server.com?subject=CONTENTOFSUBJECT&body=CONTENTOFBODY
OR
var targetEmail = "email@server.com";
var subjectLine = "name" + " " + this.getField("CLINIC NAME").valueAsString + " " + this.getField("DATE").valueAsString ;
var body = "CONTENT OF EMAIL";
this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});
1 Correct answer
It should be:
if (validateRequiredFields(this)) {
// all of the code to email the file should go here
}
Copy link to clipboard
Copied
#2 is not possible if you use the "Submit a Form" command, but it is if you open that URL using the launchURL method, but that won't solve your main problem (validating required fields).
#1 is possible. I've posted code here that does that validation with a script. Search for "validateRequiredFields".
Copy link to clipboard
Copied
Hello, I found the code you are referencing and tried to combine it with the code I already had to create an email with fields in the subject line. I'm attempting to prevent an email from popping up if there are empty required fields, but now if they have filled out correctly my code produces two emails. How do I combine so that only one email is produced? Thank you,
if (validateRequiredFields(this)) this.mailDoc({cTo: "XYZ@server.com"});
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;
}
var targetEmail = "XYZ@server.com";
var subjectLine = "OD Order Form" + " " + this.getField("CLINIC NAME").valueAsString + " " + this.getField("DATE").valueAsString ;
var body = "TEXT";
this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});
Copy link to clipboard
Copied
Replace the first mailDoc with the lines at the end.
Copy link to clipboard
Copied
It should be:
if (validateRequiredFields(this)) {
// all of the code to email the file should go here
}
Copy link to clipboard
Copied
Read this:
https://acrobatusers.com/tutorials/submitting-data/
It shows how to use the "submitForm" function, which validates required fields and allows for the email "subject", "to", and "message" text to be set. And it will submit the entire PDF.
You could also use the same code you've posted with the "mailDoc" function, but modified to validate the required fields in the script, as Try67 suggests.
Use the Acrobat JavaScript Reference early and often

