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

Forcing datafield to be null with Javascript

New Here ,
Jul 24, 2018 Jul 24, 2018

Hello all.

I am learning a bit of Javascript and have come up with the script below, however i cannot get the final line to work correctly. Is 'null' incorrect? I have a pdf form with several data fields and am trying to force certain answers when the user selects an option. For example below, when the user selects 'Regal', the next two data fields will be populated with N/A. However i still want the user to be able to select the 'Tap hole' option and henceforth need this datafield to be blank. If i just leave this line out it seems to break my form. Any help is much appreciated. Thanks

if (event.value=="Regal") {

          this.getField("Top Colour").setItems(["N/A"]);

this.getField("Basin").setItems(["N/A"]);

        this.getField("Tap Holes").setItems("null");

TOPICS
PDF forms
2.6K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Jul 24, 2018 Jul 24, 2018

Yes, it's incorrect.

Either use this:

this.getField("Tap Holes").setItems([""]);

Or this:

this.getField("Tap Holes").clearItems();

View solution in original post

Translate
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 ,
Jul 24, 2018 Jul 24, 2018

Yes, it's incorrect.

Either use this:

this.getField("Tap Holes").setItems([""]);

Or this:

this.getField("Tap Holes").clearItems();

Translate
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
New Here ,
Jul 24, 2018 Jul 24, 2018

Thanks @try67 thats worked perfectly.

I am also running this javascript i found online for the submit button however the last part of the script isn't work. It does everything except for emailing the order. Is there something that stands out as been incorrect?

app.runtimeHighlight = false;

var emptyFields = [];

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

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

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

if (( f.value=="") || (f.value==" ")){

f.strokeColor = color.red; //use this to change to the colour to require

emptyFields.push(f.userName);

}

}

}

if (emptyFields.length>0) {

app.alert({

cMsg: "Please fill out the follwing fields before submission:\n\n" + emptyFields.join("\n"), cTitle: "Required Fields"})

}

else {

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

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

if (f.type != "Submit")

{

f.readonly = true;

}

}

this.mailDoc({

cTo: "email@myemail.com",

cSubject: "This is your email subject",

cMsg: "This is your email body message"

});

}

Translate
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 ,
Jul 24, 2018 Jul 24, 2018

This is based off of code I wrote, but someone added parts to it which are incorrect.

Do you want to set all the fields as read-only before submitting the file?

Also, the first line of the code disables the fields highlighting for all documents. It's not a good idea to include it, generally speaking.

Translate
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 ,
Jul 24, 2018 Jul 24, 2018

Why not simply use the built-in Submit a Form command?

Translate
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
New Here ,
Jul 24, 2018 Jul 24, 2018

Sorry, i was using it because it removed the red border around every required field when the form was opened (as well as the blue box) and meant that the red border only reappeared around empty data fields when the user tried to submit the form. Is there anyway i can remove the highlighting from this document only rather that the whole application?

Translate
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 ,
Jul 24, 2018 Jul 24, 2018

No, but you can inform the user about it and suggest to them to turn it off.

You didn't answer about whether you want to set the fields as read-only, as that code also (attempts) to do that...

Translate
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
New Here ,
Jul 24, 2018 Jul 24, 2018

Yes i was changing all the fields to read only when the client tries to submit it. I guess it was a way to lock the details in the form before the client emails it.

Translate
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 ,
Jul 24, 2018 Jul 24, 2018

OK, try this version of that code, then:

var emptyFields = [];

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

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

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

        if (( f.value=="") || (f.value==" ")){

            f.strokeColor = color.red; //use this to change to the colour to require

            emptyFields.push(f.userName);

        }

    }

}

if (emptyFields.length>0) {

    app.alert({ cMsg: "Please fill out the follwing fields before submission:\n\n" + emptyFields.join("\n"), cTitle: "Required Fields"});

} else {

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

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

        f.readonly = true;

    }

    this.mailDoc({

        cTo: "email@myemail.com",

        cSubject: "This is your email subject",

        cMsg: "This is your email body message"

    });  

}

Translate
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
New Here ,
Jul 25, 2018 Jul 25, 2018

You are a legend. That works pefectly, Thank you.

Translate
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 ,
Jul 25, 2018 Jul 25, 2018
LATEST

I didn't notice it before, but I would change line #5 in the code above from this:

if (( f.value=="") || (f.value==" ")){

To this:

if (f.valueAsString==f.defaultValue) {

Translate
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