0 Replies Latest reply on Oct 20, 2016 12:31 PM by UniCav

    Validate required fields before save and email

    UniCav

      I have a data input form, the first of several planned, with various text, checkbox and radio fields. I'm using the "Required" attribute on several fields. I have a couple of checkboxes that use a custom validation to mark others required if they're checked. All working fine at this point as long as I use a Submit Form action. Now I'm trying to develop a javascript button for "Save and Send". Of course that eliminates the SubmitForm required field checking and I have to add that into my processing script. I have the initial code working which flattens or marks readonly and creates the email with subject and message using some field data for identification.

       

      // Flatten the form in Acrobat or mark READONLY in Reader
      flattenFields()
      
      function flattenFields() {
          if (app.viewerType=="Reader") {
              for (var i=0 ; i<this.numFields ; i++) {
                  var f = this.getField(this.getNthFieldName(i)) ;
                  if (f==null) continue;
                  f.readonly = true;
              }
          } else {
              this.flattenPages();
          }
      }
      
      // Email the form
      // Recipient Email
      var cToAddr = "email@somedomain.com"
      
      // The Employee Name and Effective Date for the Subject and Message
      var cEmployee = this.getField("Employee.Name").value;
      var cDate = this.getField("Effective.Date").value;
      
      // Set the subject and body text for the email message
      var cSubLine = "BRR Data Input Form-" + cEmployee + "-" + cDate; 
      var cBody = "Data Input Form - " + cEmployee + " - Effective Date " + cDate;
      
      // Send the entire PDF as a file attachment on an email
      this.mailDoc({bUI: true, cTo: cToAddr, cSubject: cSubLine, cMsg: cBody});
      

       

      Now I'm trying to add a function at the top to validate all the fields marked required before it will proceed. I've followed several different examples and at one time I had a working test using this code

      // Check for empty required fields
       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.visible) {
                 if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);
            }
       }
       if (emptyFields.length>0) {
            app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));
       }  else {
      
      // rest of processing and email
      }
      
      

       

       

      Then I realized that once the validation passes it won't do anything else. No prompts, no email, nothing.

      Any suggestions would be appreciated.