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

Form sent even though required fields are not filled in

Community Beginner ,
Sep 18, 2022 Sep 18, 2022

Copy link to clipboard

Copied

Hello all,

I am a relative newbie when it comes to this, so any help would be greatly appreciated. I have created a form where a user fills out a form, clicks submit for approval, then it will send the form in an email to the person that has to approve it.  They fill it out click on 'submit for approval,' and the form is attached to an email that opens up, ready to enter in the approver's email address. 

Once the approver receives it and opens it, they fill out the remaining fields of the form (hidden until the user clicks on 'submit for approval', then click on 'submit approved form.' 

With the help of Try67, I was able to get this part working correctly but I just noticed that if I fill in nothing and click on 'submit for approval,' the email is generated with the blank form attached, even though most of the fields are required.

What I would like to happen is that the fields the approver has to fill in would be required, but if I set them as required, the first 'submit for approval' will not generate the email because required fields are missing. Well, that is what was happening before but does not happen now. This was what I was trying to find out when I just decided to see if it would send without filling in anything, and it did! Why is this happening?

 

Ki 

TOPICS
JavaScript , PDF forms

Views

1.2K

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 , Sep 23, 2022 Sep 23, 2022

Place this part of the script as a calculation script in any text field, you just have to change the field's name:

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required && f.value == f.defaultValue) {emptyFields.push(f.name);}
}

if (emptyFields.length>0) {this.getField("submitbutton").display = display.hidden}
else {this.getField("submitbutton").display = display.visible}

 

 

And use the other part of the sc

...

Votes

Translate

Translate
Community Beginner ,
Sep 18, 2022 Sep 18, 2022

Copy link to clipboard

Copied

Kibang_0-1663557372539.png

All the fields below 'submit for approval' should be required. I made them visible for reference purposes, but they are usually hidden until the 'submit for approval' button is pressed and the initial email with attachment is generated. 

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 ,
Sep 19, 2022 Sep 19, 2022

Copy link to clipboard

Copied

If you're not using the embedded "Submit a form" feature (see capture) you must use JavaScript to manage your own required fields checker.

 

Capture_900.png

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 ,
Sep 19, 2022 Sep 19, 2022

Copy link to clipboard

Copied

@JR Boulay , thank you for letting me know. I was able to find a script that mostly addresses what I need, but I have another issue with the form. I have it set so that when the 'submit for approval' button is clicked, that button disappears so that it will not confuse the person approving the form. How can I prevent that from being hidden if the user has not filled in all required fields?

 

Ki

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 ,
Sep 19, 2022 Sep 19, 2022

Copy link to clipboard

Copied

Please share your 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 ,
Sep 19, 2022 Sep 19, 2022

Copy link to clipboard

Copied

Hi JR,

 

Here it is. Thank You!

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required ) {

if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

var firstname = this.getField("First Name").value;
var lastname = this.getField("Last Name").value;
var emailaddress = this.getField("Email Address").value;
var divisionemail = this.getField("Division").value;
var projectcode = this.getField("Project Code").value;
var departuredate = this.getField("Departure Date").value;
var returndate = this.getField("Return Date").value;
var departureairport = this.getField("Departure Airport").value;
var arrivalairport = this.getField("Arrival Airport").value;

if(firstname != "" && lastname != "" && emailaddress != "" && divisionemail != "" && projectcode != "" && departuredate != "" && returndate != "" && departureairport != "" && arrivalairport != ""){
this.getField("Submit for Approval").display = display.visible;
else {
this.getField("Submit for Approval").display = display.hidden;
}

// This is the form return email. It's hardcoded
// so that the form is always returned to the same address.
// Change address on your form to match the code below
var cToAddr = ""

// First, get the client CC email address
var cCCAddr = this.getField("Email Address").value;


// Set the subject and body text for the email message
var cSubLine = "Request for approval of travel authorization form for " + this.getField("First Name").valueAsString + " " + this.getField("Last Name").valueAsString

var cBody = "Please fill in the Approved By, Date, and Signature (NOTE: you may have to create a digital signature), then click on Submit Approved Form."

// Send the entire PDF as a file attachment on an email
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});
}

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

Your script builds an array with the names of the empty required fields but it does not use them!

Try this one:

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required && f.value == f.defaultValue) {

emptyFields.push(f.name);

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

else {
var firstname = this.getField("First Name").value;
var lastname = this.getField("Last Name").value;
var emailaddress = this.getField("Email Address").value;
var divisionemail = this.getField("Division").value;
var projectcode = this.getField("Project Code").value;
var departuredate = this.getField("Departure Date").value;
var returndate = this.getField("Return Date").value;
var departureairport = this.getField("Departure Airport").value;
var arrivalairport = this.getField("Arrival Airport").value;

if(firstname != "" && lastname != "" && emailaddress != "" && divisionemail != "" && projectcode != "" && departuredate != "" && returndate != "" && departureairport != "" && arrivalairport != ""){
this.getField("Submit for Approval").display = display.visible;
else {
this.getField("Submit for Approval").display = display.hidden;
}

// This is the form return email. It's hardcoded
// so that the form is always returned to the same address.
// Change address on your form to match the code below
var cToAddr = ""

// First, get the client CC email address
var cCCAddr = this.getField("Email Address").value;


// Set the subject and body text for the email message
var cSubLine = "Request for approval of travel authorization form for " + this.getField("First Name").valueAsString + " " + this.getField("Last Name").valueAsString

var cBody = "Please fill in the Approved By, Date, and Signature (NOTE: you may have to create a digital signature), then click on Submit Approved Form."

// Send the entire PDF as a file attachment on an email
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});
}
}

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

JR,

Thank you for the code. I am getting an error while trying to add the script. The line in red below is what is highlighted in the screenshot. As I learn more and more, I realize there is so much more to learn.

I am guessing the curly brace on line 13 opens the validation of required fields, then another on line 24 opens whether the button will appear or not, with another open curly brace on 26, and closed on line 28 if there is a required field not filled in to continue hiding the button, and then the whole part of this script closing at the end on line 46, but I am not sure why there is another closing curly brace on line 47. I am not sure if this makes sense ( I did try to figure it out before reaching out again) and I may just be talking out of my Ass-cot, so please bear with me. 

Kibang_0-1663680550234.png

else {
var firstname = this.getField("First Name").value;
var lastname = this.getField("Last Name").value;
var emailaddress = this.getField("Email Address").value;
var divisionemail = this.getField("Division").value;
var projectcode = this.getField("Project Code").value;
var departuredate = this.getField("Departure Date").value;
var returndate = this.getField("Return Date").value;
var departureairport = this.getField("Departure Airport").value;
var arrivalairport = this.getField("Arrival Airport").value;

if(firstname != "" && lastname != "" && emailaddress != "" && divisionemail != "" && projectcode != "" && departuredate != "" && returndate != "" && departureairport != "" && arrivalairport != ""){
this.getField("Submit for Approval").display = display.visible;
else {
this.getField("Submit for Approval").display = display.hidden;
}

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

Just wanted to add I frankensteined different scripts here from other posts (Thanks CG Art Designs and Thom Parker!). There is no way I could have come up with this myself.

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

++ Adding to the always valuable guidance of JR Boulay and others

 

I was observing about how the curley braces are employed with the If/Else  conditions. 

 

For example, see below

 

This line: 

 

if (firstname != "" && lastname != "" && emailaddress != "" && divisionemail != "" && projectcode != "" && departuredate != "" && returndate != "" && departureairport != "" && arrivalairport != "") {
this.getField("Submit for Approval").display = display.visible;
else {
this.getField("Submit for Approval").display = display.hidden;
}

 

Maybe change it to:

if (firstname != "" && lastname != "" && emailaddress != "" && divisionemail != "" && projectcode != "" && departuredate != "" && returndate != "" && departureairport != "" && arrivalairport != "")  {


this.getField("Submit for Approval").display = display.visible;

 

} else {


this.getField("Submit for Approval").display = display.hidden;

 

    }

 

 

 

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 ,
Sep 21, 2022 Sep 21, 2022

Copy link to clipboard

Copied

@ls_rbls, thank you for the input. I was still getting syntax errors so its in another part of the 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 Expert ,
Sep 21, 2022 Sep 21, 2022

Copy link to clipboard

Copied

Sorry, I added orange lines without checking the rest of your script, some curly braces were missing.

This one should works:

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required && f.value == f.defaultValue) {emptyFields.push(f.name);}
}

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

else {
var firstname = this.getField("First Name").value;
var lastname = this.getField("Last Name").value;
var emailaddress = this.getField("Email Address").value;
var divisionemail = this.getField("Division").value;
var projectcode = this.getField("Project Code").value;
var departuredate = this.getField("Departure Date").value;
var returndate = this.getField("Return Date").value;
var departureairport = this.getField("Departure Airport").value;
var arrivalairport = this.getField("Arrival Airport").value;

if(firstname != "" && lastname != "" && emailaddress != "" && divisionemail != "" && projectcode != "" && departuredate != "" && returndate != "" && departureairport != "" && arrivalairport != ""){
this.getField("Submit for Approval").display = display.visible;
}
else {
this.getField("Submit for Approval").display = display.hidden;
}

// This is the form return email. It's hardcoded
// so that the form is always returned to the same address.
// Change address on your form to match the code below
var cToAddr = ""

// First, get the client CC email address
var cCCAddr = this.getField("Email Address").value;


// Set the subject and body text for the email message
var cSubLine = "Request for approval of travel authorization form for " + this.getField("First Name").valueAsString + " " + this.getField("Last Name").valueAsString

var cBody = "Please fill in the Approved By, Date, and Signature (NOTE: you may have to create a digital signature), then click on Submit Approved Form."

// Send the entire PDF as a file attachment on an email
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});
}

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 ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

JR,

Thank you. That fixed it, but it is not doing what it is supposed to. I tested it and left a required field blank. When I clicked on 'Submit for Approval', the hidden fields showed, and the error message popped up saying the field was required. I thought it might be an issue where I needed to hide the button to begin with, so I did that. The problem is that when I fill in all the required fields, nothing happens. I would like it to show the Submit for Approval once all required fields have been filled in, or absent that, for the hidden fields to not show until all the required fields are filled in and the Submit for Approval button has been pressed.

In essence, it's two parts; user fills out form and clicks 'submit for approval', which generates email to go to approver. They open it, fill in their fields then click 'submit approved form' which generates another email that has pre-assigned email addresses and form dependent email addresses in the To: and Cc: fields. 

So if the 'submit for approval' button goes away, then only the 'submit approved form' button shows. How would I get it so that the 'submit for approval' button remains or appears once the missing required fields are filled in? 

 

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 ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Just wanted to clarify. The second generated email happens when the 'submit approved form' button is pressed. It is not part of 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 Expert ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

Place this part of the script as a calculation script in any text field, you just have to change the field's name:

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required && f.value == f.defaultValue) {emptyFields.push(f.name);}
}

if (emptyFields.length>0) {this.getField("submitbutton").display = display.hidden}
else {this.getField("submitbutton").display = display.visible}

 

 

And use the other part of the script to manage the 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 Beginner ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

LATEST

JR, 

Thank you so much for all of your help. It was a lot. I really appreciate 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