Copy link to clipboard
Copied
I am currently working on a pdf form that uses Javascript. I have a Javascript setup to validate that all required fields are filled in, followed by a save as, and then followed by email that populates the subject field using form fields. There are three javascripts built into one button. Is there anyway that I can put a stop in that if all required form fields aren't filled in that the other two javascripts won't run?
Use this code:
...var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
} else {
app.execMenuItem("SaveAs");
var parts = this.getField("Job Name").valueAsString;
var kind = this.getField("Today's Date").v
Copy link to clipboard
Copied
Not if they are separate items, no. But it should be pretty simple to combine them all into a single script, and then it will be possible.
Copy link to clipboard
Copied
ok and how do I put a stop into my javascript that would do this?
Copy link to clipboard
Copied
Using an if-condition or by placing the code in a function and calling the
return-command.
Copy link to clipboard
Copied
You can't put in a stop, so you just don't do the things you don't want to happen.
if ( we_dont_have_to_stop )
{
do_the_other ( things ) ;
}
Copy link to clipboard
Copied
This is what my javascript looks like
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);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
}
Copy link to clipboard
Copied
That's the first part, presumably. You say you have three pieces of JavaScript. You need to put the remaining two parts into an "else".
Copy link to clipboard
Copied
Currently one button does three things consecutively not in the same script.
The next thing would just be execute a menu item > File > Save as > PDF
and the last part of my javascript would be this
var parts = getField("Text Field 1").value
var kind = getField("Text Field 2").value
var date = getField("Holiday").value
this.mailDoc({
cTo: "Becky.Tonn@email.com",
cSubject: parts + " - " + kind + " - " + date,
cMsg: "A new request has been sent from sales."
});
Copy link to clipboard
Copied
All the parts need to be JavaScript. So see the saveAs method in the document class.
Copy link to clipboard
Copied
They won't be able to use saveAs without a doc-level script. Instead, use this:
app.execMenuItem("SaveAs");
Copy link to clipboard
Copied
Sorry but I am a little lost on this. Here is what I have setup so far in my Notepad. Where would I add the part about the save as? Sorry I am a graphic designer and not a programmer.
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);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
}
var parts = getField("Job Name").value
var kind = getField("Today's Date").value
var date = getField("Holiday").value
this.mailDoc({
cTo: "Becky.Tonn@bicgraphic.com",
cSubject: parts + " - " + kind + " - " + date,
cMsg: "A new request has been sent from sales."
});
Copy link to clipboard
Copied
Use this code:
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
} else {
app.execMenuItem("SaveAs");
var parts = this.getField("Job Name").valueAsString;
var kind = this.getField("Today's Date").valueAsString;
var date = this.getField("Holiday").valueAsString;
this.mailDoc({
cTo: "Becky.Tonn@bicgraphic.com",
cSubject: parts + " - " + kind + " - " + date,
cMsg: "A new request has been sent from sales."
});
}
Copy link to clipboard
Copied
This works perfectly. Thank you for the help.
Copy link to clipboard
Copied
Can't saveAs be used without a filename and do a prompt, in just the same way as the execMenuItem (though that's a good solution I'd forgotten!)?
Copy link to clipboard
Copied
https://forums.adobe.com/people/Test+Screen+Name wrote
Can't saveAs be used without a filename and do a prompt, in just the same way as the execMenuItem (though that's a good solution I'd forgotten!)?
No, the cPath parameter is required.
Copy link to clipboard
Copied
Like I said, you add an else
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
}
is what you want to happen if it FAILS. If it PASSES you continue to
else
{
do stuff ;
more stuff ;
}
It all goes in there
Copy link to clipboard
Copied
Also you didn't add the code we gave you for replacing the save as.