Expand my Community achievements bar.

SOLVED

Bypass Required Field Validation when needed in PDF Dynamic Form

Avatar

Level 8

I faced a tricky situation, where some fields are required, but we need to allow bypass required (mandatory) validation rule when saving the form, and require to fill such fields when submitting the form. In other words, provide flexible control when to turn On / Off this feature.

I wanted to implement a flexible solution, and I will post my findings here. Appreciate your feedback for improvements.

Steps:

1. Mark rquired fields as required.

2. Specify "Empty Message" as "This field cannot be left blank", or similar.

3. Specify "Validation Script Message" as "This field must have a proper value before submit", or similar.

4. Create a Global Form Level Variable something like "StopTotalValidation" and default as "1" means by default, Turn Off Validation for some cases.

5. For the fields which require this type of control, add the script (to be defined later) on the "validate" event:

myTools.validateForRequiredField(this);

6. Create a Script Object "myTools" and add the following script:

function initStringFunc() {

//call this function on Document Initialize

String.prototype.trim = function() {

    return this.replace(/^\s+|\s+$/g,"");

}

String.prototype.ltrim = function() {

    return this.replace(/^\s+/,"");

}

String.prototype.rtrim = function() {

    return this.replace(/\s+$/,"");

}

String.prototype.isEmpty = function() {

    return (this == null) || this.trim() == "";

}

}

function setNodeProperty(theNode, theProperty, newValue) {

   if (theNode[theProperty] != newValue) {

        theNode[theProperty] = newValue; 

    }

}

function isNodePropertyEmpty(theNode, theProperty) {

    var result;

    if (theNode == null || theNode[theProperty] == null) {

        result = true;

    } else {

        result = theNode[theProperty].isEmpty();

    }

    return result;

}

function disableTotalValidation() {

    StopTotalValidation.value = "1";

}

function enableTotalValidation() {

    StopTotalValidation.value = "0";

}

function isTotalValidationOn() {

    return StopTotalValidation.value != "1";

}

function isTotalValidationOff() {

    return StopTotalValidation.value == "1";

}

const conRequired = "(required)";

function validateForRequiredField(theFld) {

    //

    // Bypass Required Field Validation when Global Validation is Off.

    //

    var result=false;

    if (theFld) {

        if (theFld.mandatory && theFld.mandatory == "error") {

            if (myTools.isNodePropertyEmpty(theFld, "rawValue")) {

                myTools.setNodeProperty(theFld, "rawValue", conRequired);

            }

            if (isTotalValidationOn()) {

                if (isNodePropertyEmpty(theFld, "rawValue") || theFld.rawValue.toLowerCase() == conRequired.toLowerCase()) {

                    result = false;

                } else {

                    result = true;

                }

            } else {

                result = true;             

            }

        }

    } else {

        result = false;

    }

    return result;

}

7. Now, on the click of "Save" button call the function "disableTotalValidation()" and on the click of "Submit" button call the function "enableTotalValidation()".

I have just finished implementing the above solution, and as per my initial testing, it is working fine.

I will post this to my Google Docs workspace, and provide updates their.

Tarek.

1 Accepted Solution

Avatar

Correct answer by
Level 8

Hi Niall,

Actually, it is not that some require and some don't require validation ...!

The fact is that those fields which are "Mandatory" rule should ignored only and only if the Form is being "Saved" from the "Save" button.

The "Mandatory" rule should be triggered only and only of the user is clicking the "Submit" button.

I think I can make use of your approach. The only trouble I can see now is how to mark the <desc> element of such fields with the requied details from the LiveCycle Desinger UI ? This means, I have to call some script on the initialize event of such fields, which is more work and more confusion for other developers. As I am using a non-standard feature from the Desinger UI.

In my case, I am adding a simple call from the "validate" event, to a common function for any field which need to follow this simple rule "bypass required validation ..."

But, I really like your solution. Actually, I understand what you are doing, but the following questions are now triggered :

1. What is the meaning of three time equal (===) in the javascipt "if" statement?

2. I need more information about how to use "createNode()" and "namedItem()" methods.

Tarek.

View solution in original post

3 Replies

Avatar

Level 10

Hi Tarek,

That is a comprehensive solution. I wonder if you set an a form variable in the object's <desc> element would it be easier to identify fields that require/don't require validation. http://assure.ly/x1QObn.

Niall

Avatar

Correct answer by
Level 8

Hi Niall,

Actually, it is not that some require and some don't require validation ...!

The fact is that those fields which are "Mandatory" rule should ignored only and only if the Form is being "Saved" from the "Save" button.

The "Mandatory" rule should be triggered only and only of the user is clicking the "Submit" button.

I think I can make use of your approach. The only trouble I can see now is how to mark the <desc> element of such fields with the requied details from the LiveCycle Desinger UI ? This means, I have to call some script on the initialize event of such fields, which is more work and more confusion for other developers. As I am using a non-standard feature from the Desinger UI.

In my case, I am adding a simple call from the "validate" event, to a common function for any field which need to follow this simple rule "bypass required validation ..."

But, I really like your solution. Actually, I understand what you are doing, but the following questions are now triggered :

1. What is the meaning of three time equal (===) in the javascipt "if" statement?

2. I need more information about how to use "createNode()" and "namedItem()" methods.

Tarek.

Avatar

Level 10

Hi Tarek,

I see what you mean in relation to clarity if you used the form variable approach. It was only a suggestion. Like so many things in LC, there is more than one way to finding a solution to a problem.

The triple equal sign (===) is testing if the condition is equal, but to a higher standard. It is testing if the values are identical to each other. For example if you were testing if a textfield was empty, with Equality (==) you might have this:

if (this.rawValue == null || this.rawValue == "") {

     // Some script

}

If you use Identity (===) you can do the same thing with less script:

if (this.rawValue === null) {

     // Some script

}

It is also useful when testing the value of an object, but also the type (eg string, number, Boolean).

Lastly, it can be used for non-identity (!==).

In relation to createNode() etc, apart from John's blog, it is covered in the LC documents: http://www.adobe.com/support/documentation/en/livecycle/documentation.html. Look for the scripting guides and the guide to the XML Form Object Model.

Good luck,

Niall