Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Help - Validation fails stop script

erddtr1
Level 1
Level 1

Hi guys,

I need help with some code.

I have created a form which users a normal button as a submit for e-mail. The reason I have done this is because the button pulls out info from the form and inserts it into the e-mail. All works so far. The form contains fields which must be filled in, however because i am using a normal button when it is clicked it seems to bypass the validation process.

I have inserted - if (topmostSubform.execValidate()) at the top of my script and added in the form properties turned on form validation. If I click the submit button now I get a warning message and the required fields turn blue. However, when I click ok on the pop up message the script runs anyway and the outlook draft box appears with the e-mail.

I would like to ask - how do i stop the script running if validation fails?

0 Replies
Magus069
Level 10
Level 10

I personnally don't like auto form validation, there is too many actions I don't want it to do... especially if there's any field hidden

I suggest you to make you own validation with a bool var

Turn off form validation

if all your fields are not null, keep going until you reach the end of validation

check if all your values are null, if one is null, just put false in your bool var and skip all the validation

if any field is null, store his somExpression in a var like so: var strExpression = Reference_Syntax.somExpression;

at the end if your bool var is false, return a focus on field like so: xfa.host.setFocus(strExpression);

If there's any error in your validation you can always use xfa.host.messageBox(strExpression); to see where your validation fails

otherwise if you don't want to make your own validation try this:

use execValidate() on your form1... the form that contains Master Pages and Main page

erddtr1
Level 1
Level 1

Hi Magus069

Many thanks for your reply. However, I don't quite follow. Where do I put the validation code? Where would I put the execValidate() code? If I used the execValidate code would that stop the rest of the code running?

At the moment there are mandatory fields in the form which must be filled in. When the user clicks the submit button which is a normal regular button an e-mail is produced which lifts information directly from the e-mail. At the moment the user can click the submit button without filling in the form and the e-mail is still created. I just need some code which validates all the fields and if they are not filled in stops the rest of the code so an e-mail is not produced. Is this possible?

Magus069
Level 10
Level 10

Yes, well as I said you can write down your own validation code...

If you are using execValidate() and you are using it in your regular button click_event, you must :

1. Use execValidate() on your topmostSubform if its the name of your (originally) form1, which is the name of the form that contains your objects + the master pages...

if (form1.execValidate()){ //return true

     //write down your code to send via email here

} else{//return false

     //write down your validation failed code here

}

2. Make sure that no required fields are hidden, cuz execValidate() only checks out every mandatory fields, even if it's hidden

If execValidate() is not working, go in your Form Properties, Form Validation, in Show Dialog Message check ON Configure how Acrobat displays validation message boxes... Use this only if your validation is never returning true...

You can also use the Set Focus built in function, if everything is working fine

erddtr1
Level 1
Level 1

Hello,

Thanks for you help.

I'm sorry I am being really thick - I just cant get this to work

Just need the form to check the fields before submitting is there no simple way to do this?

Magus069
Level 10
Level 10

Well the simplest way to do so it's with execValidate();

or with your own validation

sample for creating your own validation:

var boValidation = true;     //Set your boolean value to true which means your validation is good

var strExpression = "";      //Set your Expression string to follow up your validation easily

//Now create your validation below

//For the first field just right it down as simple as this

if (TextField1.rawValue == null){

     boValidation = false;

     strExpression = TextField1.somExpression

}

//For all the following fields, look if your validation has failed, if it did failed, you don't need to keep going

if (!boValidation){

     if (TextField2.rawValue == null){

          boValidation = false;

          strExpression = TextField2.somExpression;

     }

}

if (!boValidation){

     if (TextField3.rawValue == null){

          boValidation = false;

          strExpression = TextField3.somExpression;

     }

}

//Write down all your validations tests

...

...

...

//Once everything is set to validate your form

//If your validation has passed the test, execute send email

if (boValidation){

     //send email here

} else{    

     //If the validation test has failed, execute the field focus here

     xfa.host.setFocus(strExpression);

     xfa.host.messageBox("One or more mandatory field(s) has failed validation, please revise the form.", "Validation", 2);

    

     //Use this messageBox if you are encountering problem with your validation

     //xfa.host.messageBox(strExpression);

}

Canuig
Level 1
Level 1

Hi

This is an old thread but I had an almost identical problem. Unfortunately I could not get execValidate() to function as expected. However, I used your approach above and it worked perfectly.

Many thanks for posting this approach.

Magus069
Level 10
Level 10

Sure this is a good approach, if you want to make it much easier, you can use my function I created for any form validation.

Customizable and save a lot of time. There is also a function for resetting form.

The reset function have a small bug trying to reset a subform with instances... To fix it, just need to remove xfa.host.resetData.

Resources for from design - best practices for layout?