These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Hide Form Field when Constituent Fields are Unfilled

dpkeidl
Registered: Jun 12 2008
Posts: 11
Answered

Greetings.

I'm trying to do something that seems like it should be quite simple. I have three form fields in my pdf. One of them is set to calculate the product of the other two for its value. We'll call the field that does the calculation TotalPriceA and the fields that it multiplies are QuantityA and UnitPriceA.

Unfortunately, the form must be able to be filled out digitally or by hand, which means that the $0.00 that appears in TotalPriceA is a drawback, since it would be in the way if someone wants to pencil in the total theirselves.

Thus, my problem is that I want the $0.00 to be invisible unless it's more than 0 (i.e., if either of the other fields are filled in digitally).

Based on something I found in Stefan Cameron's blog, I generated this tiny bit of script and placed it in the "custom calculation script" under the "Calculate" tab of the TotalPriceA's properties:

if (TotalPriceA > 0) then
$.presence = "visible";
UnitPriceA * QuantityA;
else
$.presence = "invisible";
0;
endif

Unfortunately I don't know if this would work, because I get an error before it'll let me click Ok.

It tells me, most helpfully:

SyntaxError: syntax error
4: at line 5

Which is the line that contains
$.presence = "invisible";

Any ideas what's wrong with this? I'm very new to scripting results and I don't know if there might not be a much easier way to accomplishing my goals. A heads-up on what I've coded wrong or a totally different approach would be equally appreciated.

Thanks!

My Product Information:
Acrobat Standard 7.0, Macintosh
naomi_basickes
Registered: Apr 19 2006
Posts: 1
Hello,
Setting the value for TotalPriceA to the empty string "" instead of 0 should keep you from getting $0.00 in the field. Try the following javascript in the "custom calculation script" under the "Calculate" tab for TotalPriceA:

//set up variables
var QuantityA=this.getField("QuantityA").value;
var UnitPriceA=this.getField("UnitPriceA").value;
var CalcTotalPriceA=QuantityA * UnitPriceA;

//set TotalPriceA field
TotalPriceA = this.getField("TotalPriceA");
if (CalcTotalPriceA > 0){
TotalPriceA.value = CalcTotalPriceA;
}else{
TotalPriceA.value = "";
}

Good luck,
Naomi Basickes
Appligent, Inc
www.appligent.com
dpkeidl
Registered: Jun 12 2008
Posts: 11
Unfortunately, no, it doesn't work. The field is indeed blank when it's empty, but it does not calculate the values of the other fields. It does not respond to the other fields being filled at all. Also, if I fill the field manually with 0, it shows $0.00.

Is this a problem related to when the script is called?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Since you are using a MacIntosh, LiveCycle Designer is not available and the code that you posted is for LiveCycle Designer FormCalc.

When testing numeric fields, one needs to convert the value to a string. If the calculation scirpt is updating the focused field, one should use the "event.value" and not the field name.

// clear this field with supressed zero value
event.value = '';

//set up variables
var QuantityA = this.getField("QuantityA");
var UnitPriceA = this.getField("UnitPriceA");

if (QuantityA.valueAsString != '' & UnitPriceA.valueAsString != '') {
event.value = QuantityA.value * UnitPriceA.value;
}

Since this basi calculation can be used many times within an order sheet, one could create a document level script to process passed values and return the necessary result and the custom calculation script can be simplified to just changing the row identifier.

function Extend(Quantity, Price) {
// function to compute the product of two values

var Extension = ''; // default action if no value

// compute extenstion only if there are values
if (Quantity.toString() != '' & Price.toString() != '') {
Extension = Number(Quantity) * Number(Price);
}

return Extension; // return default value or extended value
} // end function


// calculation script for each field. Just change the "Row" value as needed
var Row = "A"
event.value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);

And if one wanted to perform this script as part of a total field for rows "A" - "F":

var Row = "A";
this.getField("TotalPrice" + Row).value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);
event.value = Number(this.getField("TotalPrice" + Row).value);

Row = "B";
this.getField("TotalPrice" + Row).value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);
event.value = Number(this.getField("TotalPrice" + Row).value) + event.value;

Row = "C";
this.getField("TotalPrice" + Row).value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);
event.value = Number(this.getField("TotalPrice" + Row).value) + event.value;

Row = "D";
this.getField("TotalPrice" + Row).value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);
event.value = Number(this.getField("TotalPrice" + Row).value) + event.value;

Row = "E";
this.getField("TotalPrice" + Row).value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);
event.value = Number(this.getField("TotalPrice" + Row).value) + event.value;

Row = "F";
this.getField("TotalPrice" + Row).value = Extend(this.getField("Quantity" + Row).value, this.getField("Price" + Row).value);
event.value = Number(this.getField("TotalPrice" + Row).value) + event.value;

if (event.value.ToString() == '') event.value = '';

George Kaiser

dpkeidl
Registered: Jun 12 2008
Posts: 11
This script worked as-is; thanks!

gkaiseril wrote:
// clear this field with supressed zero value
event.value = '';

//set up variables
var QuantityA = this.getField("QuantityA");
var UnitPriceA = this.getField("UnitPriceA");

if (QuantityA.valueAsString != '' & UnitPriceA.valueAsString != '') {
event.value = QuantityA.value * UnitPriceA.value;
}
However,

gkaiseril wrote:
When testing numeric fields, one needs to convert the value to a string.
this seems like a non-intuitive restriction. Would it not be easier (less script, less processing power) to compare reals than strings?

gkaiseril wrote:
Since this basi calculation can be used many times within an order sheet, one could create a document level script to process passed values and return the necessary result and the custom calculation script can be simplified to just changing the row identifier.
Where does one enter document-level scripts?
dpkeidl
Registered: Jun 12 2008
Posts: 11
I have a follow-up question. Using this system, if I understand it correctly, the resulting field is a string and not a number. If that's true, can the value of this field be used in additional calculations (say, as part of a sales tax calculation)?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
One has to test numbers as a string value becuase JavaScript is not a very strict about data types and will change the between string and number as it thinks fit. That is why there is a "Number()" constrictor to force values to numbers, the "toString()" method to force to a string, and "isNaN()" method to test for numbers.

Thom Parker has the [url=http://www.acrobatusers.com/tech_corners/javascript_corner/tips/index.php?sort=true&search_keyword=All]JavaScript Corner[/url]column with lots of excellent articles, including [url=http://www.acrobatusers.com/tutorials/2007/js_document_scripts/]Entering Document Scripts[/url].

George Kaiser