Expand my Community achievements bar.

Need help with a validation script

Avatar

Former Community Member
I need to develop a validation script that when a button is clicked, will check for all required fields (text, date, and radio buttons) to have an entry in them before printing is allowed. If a required field is null, I want to

-alert user

-(nice to have) highlight first field missed that is required

- set focus to that field.

If the form passes validation, it should print. Data doesn't need to be saved.

I know what a I want is a global script object-I've created that and have it attached to the form, but it doesn't show up as an available function to my button, which I thought it would. Then, there is the exact way to code the function, too.



Any tips appreciated..

thanks.
11 Replies

Avatar

Former Community Member
This is certainly achievable.



First of all, I would suggest you have a look at the sample I posted in the
Highlight Required Scripting thread. It'll show you how to find fields which are required and highlight them using a script object. You'll then need to tweak the
HighlightFields function in the script object not to highlight all required fields but rather to simply return the first one it finds whose value is null. The calling function would then highlight the field, show the error message and then set focus to the field in question.



If the
HighlightFields function returns a null object, then you would know that it didn't find any required fields which weren't filled and you could then have the calling function execute the code which will print the form:



xfa.host.print(1, "0", (xfa.host.numPages -1).toString(), 0, 1, 0, 0, 0);


Of course, you would skip that step if the
HighlightFields function returns a non-null object (which would be the non-filled required field).



Does this make sense to you? Let me know if you have any questions.



Stefan

Adobe Systems

Avatar

Former Community Member
It makes sense to me logically, but, the devil is in the details (execution, that is.)

I had to re-do my form; I discovered I botched the hierarchy; I had things on the master pages and I know that shouldn't be. I fixed that.

Then, I copied over your script object, and made a checkbox and attached the identical code.

I also made sure I have identified the required fields on the form.

I don't think the script is even envoking-I say this because I put an "app.alert("script started"); in the top of the function, and that never gets called.

I feel like a dork; out of practice on javascript. But I'm trying to get in the game again. And spending time with acrobat js guide.

Any tips appreciated.

thanks.

Avatar

Former Community Member
It can certainly be frustrating when things don't work the way we think they should. Let's see if we can figure this out.



You say that you placed a check box on your form and copied the script I had put on the "highlight required" check box on the sample form I pointed you to but this script doesn't see to be running when you check the box? Did you put the script in the Click event? Did you set the Click event's Language property to
JavaScript?



Ideally, I believe you should be using a regular button object with a "Print" caption. In the button's Click event, in JavaScript, you would place a call to



// clear the last field to have been highlighted -- if any

ScriptObject.RemoveCurrentHighlight();



var oInvalidField = ScriptObject.HighlightRequiredFields(form1, true);



if (oInvalidField != null)

{

// found an empty required field -- ask user to fill it

app.alert("The " + oInvalidField.name + " field must be filled prior to printing this form.");

xfa.host.setFocus(oInvalidField.name);

}

else

{

// all required fields are filled -- print the form

xfa.host.print(1, "0", (xfa.host.numPages -1).toString(), 0, 1, 0, 0, 0);

}


Of course, this implies that you've modified the ScriptObject.HihglightRequiredFields method to stop at the first-found required field which isn't filled and save it to a script object variable (such as ScriptObject.moInvalidField) and return it as an object so that you can set focus to it. It also implies that you've defined a new ScriptObject.RemoveCurrentHighlight() function which, if ScriptObject.moInvalidField isn't null, removes its highlight.



I should mention, however, that there will unfortunately always be a way for the user to print the form without going through your "print" button in order to do so ("Ctrl + P", "File | Print", print toolbar button, etc.) and while some of these functions can be disabled via some complex scripting, not all of them can be. You'll unfortunately have to expect that you may still get some printed forms which aren't completely filled. If it would be possible for you to accept data submitted via email or to a web server, there's more control over filling required fields that way.



Stefan

Adobe Systems

Avatar

Former Community Member
I tried doing this but it did not run properly. Did anybody get this to work?

Avatar

Former Community Member
Danny,



I apologize for taking so long to get back to this thread.



Here's a sample I built based on the instructions I posted here on [12:46pm Jun 18, 06 PST (#3 of 4)].



Please let me know if you have any questions.



Stefan

Adobe Systems

Avatar

Former Community Member
Dear Stefan,



How can I set field back to transparent? I using a background image for the whole pdf and it's really ugly, when fields are not trasnparent but with white background.

I tried insert the following rows into your highlightrequredfield.pdf example to change back the bgs



//result is white bg

oUITypeNode.border.fill.color.value = "255,255,255";



// result was nothing happened

oUITypeNode.border.fill.color = color.transparent;



// result was nothing happened

oUITypeNode.fillColor = color.transparent;



// result was black bacground (invalid color spec)

oUITypeNode.border.fill.color.value = color.transparent;



// result was black bacground (invalid color spec)

oUITypeNode.fillColor.value = color.transparent;



(maybe i mixed the results, but it doesn't matter, the point is, i cannot get back my trasparents fields)

Avatar

Former Community Member
I guess my sample is a little deceiving because it sets the fill color to white to remove the highlight instead of removing the fill completely (since the page background color is white anyway).



The proper way to do this is the use
presence attribute of the border or fill nodes:







  • If you want only the color to be transparent, do oUITypeNode.border.fill.presence = "hidden";





  • If you want the entire border (edges, fill color, etc.) to be transparent, then do oUITypeNode.border.presence = "hidden";





Note that the
color object is specific to the Acrobat Object Model and can only be used to set the color of objects in that model. In this case, you're dealing with objects in the XFA Form Object Model and therefore you have to use strings that represent RGB values.



Stefan

Adobe Systems

Avatar

Former Community Member
Dear Stefan,



After my post i found in the documentation that presence must be set to hidden.

This is still not works, on oUITypeNode, but in your HighlightOneRequredfields it worked. Don't know why.



Andras

Avatar

Former Community Member
The behaviour of the border, including its edges, can depend on a few factors. If you made the UI border hidden by choosing
None from the Appearance drop down list in the Object palette, then this would have the effect of making the UI border hidden to start with whereas selecting another value would make it visible.



When you set the fill color on a border node (like the UI border) which hasn't been specified yet, then you get all sorts of defaults along with it, including default edges, as per the XFA spec.



I hope I'm not just adding more mud to the muddy waters but maybe this will remind of you something you set on one of the fields.



Stefan

Adobe Systems

Avatar

Former Community Member
Finally afther ahours of debugging I got the solution.

If I save it as Dynamic PDF Form file, the borders can be set transparent otherwise in Static PDF Form file the borders cannot be set to transparent.

Don't ask why :).



Andras

Avatar

Former Community Member
Stefan -
I've tried incorporating this script into my LiveCycle form, but attaching it to a regular "Save Form" button. Currently, this button contains the following "on click" event:





When I try to add the event in your example, the validation does not work. (I did change the "Print" command to the "SaveAs" command.) I should mention that I also have a "Email to" button on the same form which will validate the required fields before executing the email command.