Expand my Community achievements bar.

SOLVED

Trouble with Exit event script

Avatar

Level 10

Actually I'm not sure if I'm having trouble with the Exit event or if there's something wrong with my script syntax...On the exit event of a field I'm checking to see if the field is empty or not and checking a flag I have set up.

The first If statement works - If I enter a value in the field everything goes according to plan. If I then delete the value in the field nothing happens.

//if field has a value hide subSection04

if (!(this.rawValue == null || this.rawValue.length == 0))

{

     subSections.subSection04.presence = "hidden";

}

//if field is empty and flag equals true show subSection04

else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == true))

{

     subSections.subSection04.presence = "visible";

}

//if field is empty and flag equals false hide subSection04

else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == false))

{

     subSections.subSection04.presence = "hidden";

}

There are a couple radio button groups that also control this behaviour, they all inter-relate. So I also tried firing the Click event of a radiobutton group (that looks at the value of this field) on exiting this field but that didn't seem to work.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Jono,

Your expression subFlags.Commercial.rawValue == true is comparing a String datatype with a boolean datatype.

Modify the expression as  subFlags.Commercial.rawValue == "true"

I hope this should work for you.

You code should look like

//if field has a value hide subSection04
if (!(this.rawValue == null || this.rawValue.length == 0))
{
     subSections.subSection04.presence = "hidden";
}

//if field is empty and flag equals true show subSection04
else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == "true"))
{
     subSections.subSection04.presence = "visible";
}

//if field is empty and flag equals false hide subSection04
else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == "false"))
{
     subSections.subSection04.presence = "hidden";
}

Nith

View solution in original post

27 Replies

Avatar

Level 10

Hi Jono,

Would this work?

if (this.rawValue != null || this.rawValue.length != 0)

The nested if statement looks okay to me.

Niall

Avatar

Level 10

Hey Niall, that's the part that is working...it's if I delete an existing value in the field that things aren't working - the two else...if statements.

Avatar

Level 10

Hi Jono,

The code looks good to me too, the only thing I'm not sure about is "subFlags.Commercial.rawValue == true" ... what field type is Commercial that it would have a Boolean rawValue?

Can you do a "app.alert(typeof(subFlags.Commercial.rawValue));" to check ... if it is a RadioButton it'll be a string.

Bruce

Avatar

Level 10

Hi Bruce, it's just a text field that I inject the text "true" or "false" into. I was just finding it easier to think of true or false instead of 1 or 2 for a checkbox or something like that.

Are true and false reserved words? I'm using the words as strings in a text field but could that impact things?

Avatar

Level 10

Oh, the other thing though...what about the execEvent("click") I was trying on the exit event to try and fire the radio button group that does most of the controlling - it didn't seem to do anything. I thought it would cause the radio button group to do its thing again.

Avatar

Level 10

Hi Jono,

I know what you mean about "0" and "1" being false and true, but "true" and "false" are reserved Boolean literals, so I would avoid them in this code.  It you try the following you will see what I mean.

TextField2.rawValue = true;
app.alert(TextField2.rawValue);

Because you get the value "True" displayed ... that is with a capital "T".

Not sure about the execEvent problem, though I would use the change event for a Radio Button ... maybe worth trying, happy to have a look if you want to post the form, but I'm GMT+11 so am off to bed soon.

Bruce

Avatar

Level 10

Thanks guys, I'll check it out tomorrow - got pulled on to another project today.

I was using the change event for radio buttons but switched it after Niall warned me off the change event...

Avatar

Correct answer by
Level 10

Hi Jono,

Your expression subFlags.Commercial.rawValue == true is comparing a String datatype with a boolean datatype.

Modify the expression as  subFlags.Commercial.rawValue == "true"

I hope this should work for you.

You code should look like

//if field has a value hide subSection04
if (!(this.rawValue == null || this.rawValue.length == 0))
{
     subSections.subSection04.presence = "hidden";
}

//if field is empty and flag equals true show subSection04
else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == "true"))
{
     subSections.subSection04.presence = "visible";
}

//if field is empty and flag equals false hide subSection04
else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == "false"))
{
     subSections.subSection04.presence = "hidden";
}

Nith

Avatar

Level 10

I use the change event, mainly because the click event is raised even if they click on the radio button that is already selected and sometimes that can muck the behavior of my form.

Would be interested in what problems the change event has caused ... was it a Reader version thing?

Bruce

Notice:

The information contained in this email message and any attached files may be confidential information, and may also be the subject of legal professional privilege. If you are not the intended recipient any use, disclosure or copying of this email is unauthorised. If you received this email in error, please notify the sender by contacting the DEEWR Switchboard on 13 33 97 (1DEEWR) during business hours (8am - 8pm AEST) and delete all copies of this transmission together with any attachments.

Avatar

Level 10

Hi,

No major problem with the change event - just a personal preference for that type of object.

I tend to always use the click event for radio buttons and checkboxes, as I don't need to drill down as to what the user inputted or what the actual change was - the object is either on or off. Also I find it easier to explain, as you are just dealing with the standard .rawValue, instead of introducing the concept of the event model.

Just seems cleaner to me,

Niall

Avatar

Level 10

Argh! That was it...everywhere else I was using strings but forgot the quotes in that one if statement.

Still, probably best I don't use reserved words for that kind of thing, just in case.

Avatar

Level 10

So I'm wondering if there is a better way to handle flags?

I started out trying to use form variables but they just didn't seem to work.

Avatar

Level 10

Hi Jono,

I use form variables a lot and I also add content to <desc> node of a field (which works pretty much as a form variable).  John Brinkman explained this in one of his blogs;  http://blogs.adobe.com/formfeed/2008/10/form_variables.html

I've got some script objects that make it easy enough if you want another try.

Bruce

Avatar

Level 10

I wouldn't mind taking a look at them Bruce.

I'll read the article on John's blog - I gave form variables a try at the start of working on this form but couldn't seem to get values to stick.

Avatar

Level 10

Hi Jono,

There are three functions;

setProperty(node, property, value, type) to set or update a value

     node is the field or subform to add the property to

               (fields get a element in the <desc> element, subforms get a <variable> element

     property is the name of the property

     value is the value

     type is one of the XFA types (see comments in code) ... this defaults to Text (or string)

getProperty(node, property, defaultValue) to return a value

     node is the field or subform that holds the property

     property is the name of the property

     defaultValue optional (is returned if the value does not exist)

deleteProperty to delete value

     node is the field or subform that holds the property

     property is the name of the property

So if XFAUtil was the name of the script then this code would set an integer property "selectedIndex".

     XFAUtil.setProperty(this, "selectedIndex", i, XFAUtil.ContentType.Integer);

Do an app.alert(this.saveXML("pretty")) afterwords to see what it has done.

So to get the value back use;

     XFAUtil.setProperty(this, "selectedIndex")

The value returned will be a JavaScript equivilent of the XFA type passed in, which can avoid some problem.  So if you specified Integer on the setProperty you will get a JavaScript Number value back. So in this example (if i was 10) then var si = XFAUtil.setProperty(this, "selectedIndex") + 10; would equal 20 ... not "1010" that it does if using form variables (as these are always strings).

I've been using it pretty much since John's post and haven't had any trouble.

Hope it Helps

Bruce

form1.#subform[0].#variables[0].XFAUtil - (JavaScript, client)

var ContentType = { Boolean : "boolean", // Creates an element with zero (true) or one (false)
     Date : "date", // Seems to work as a string
     DateTime : "dateTime", // Seems to work as a string
     Decimal : "decimal",
     ExData : "exData", // Seems to work as a string
     Float : "float",
     Image : "image",
     Integer : "integer", // Carefull: this is a 32 bit signed integer, JavaScript only has a float so some truncation can occur
     Text : "text",
     Time : "time" // Seems to work as a string
     }
/*
* Sets a custom property against a field or subform.
* @param {field or subform} node     The field or subform to hold the value.
* @param {string}           property The name of the property.
* @param {object}           value    The value of the property.
* @param {ContentType}      type     The type of the property.
*/    
function setProperty(node, property, value, type)
{
// Debug.trace(arguments);
var item;
if (type == undefined) type = XFAUtil.ContentType.Text;
 
if (node.className == "subform")
{
  item = node.variables.nodes.namedItem(property);
  if (item == null)
  {
      item = xfa.form.createNode(type, property);
      node.variables.nodes.append(item);
  }
}
else
{
  item = node.desc.nodes.namedItem(property);
  if (item == null)
  {
   item = xfa.form.createNode(type, property);
   node.desc.nodes.append(item);
  }
}
if (typeof(value) === "boolean")
{
  // "item.value" is typed so avoid the toString() below, otherwise we'll always be false;
  item.value = value;
}
else
{
  // Some not very JavaScript like conversions (or lack of them) going on here ... so use toString()
  item.value = value.toString();
}
}

/*
* Gets a custom property against a field or subform.
* @param {field or subform} node        The field or subform containing the value.
* @param {string}           property    The name of the property.
* @param {object}           defaultValue    The default value of the property if it does not exist.
* @return       The value of the object typed as per comments above
*/    
function getProperty(node, property, defaultValue)
{
// Debug.trace(arguments);
var childNode;
if (node.className == "subform")
{
  childNode = node.variables.nodes.namedItem(property);
}
else
{
  childNode = node.desc.nodes.namedItem(property);
}
if (childNode === null)
{
  if (defaultValue === undefined)
   return null;
  else
   return defaultValue;
}
else
{
  return childNode.value;
}
}

/*
* Deletes a custom property against a field or subform.
* @param {field or subform} node        The field or subform containing the value.
* @param {string}           property    The name of the property.
*/    
function deleteProperty(node, property)
{
// Debug.trace(arguments);
if (node.className == "subform")
{
  var item = node.variables.nodes.namedItem(property);
  if (item !== null)
  {
      node.variables.nodes.remove(item);
  }
}
else
{
  var item = node.desc.nodes.namedItem(property);
  if (item !== null)
  {
   node.desc.nodes.remove(item);
  }
}
}

Avatar

Level 10

Thanks Bruce, looks interesting. Anything I can do to extend my knowledge of javascript and xfa is a huge bonus.    

Could you give me a brief example of how you use these? Do you use it basically for setting flags or other things too?

Avatar

Level 10

Hi Jono,

I hope I haven't been sending you off in the wrong direction.  I was thinking that where the subFlags.Commercial.rawValue is set to true or false you could do something like;

     XFAUtil.setProperty(subFlags, "Commercial", true, XFAUtil.ContentType.Boolean);

Then instead of the line;

     else if ((this.rawValue == null || this.rawValue.length == 0) && (subFlags.Commercial.rawValue == true))

you would have

     else if ((this.rawValue == null || this.rawValue.length == 0) && (XFAUtil.getProperty(subFlags, "Commercial")))

I usually use this method for controlling my script fragments. I have a script to set the whole form to read only but put the line "XFAUtil.setProperty(this, "AllowAccessChange", false, XFAUtil.ContentType.Boolean);" in the initialise event of fields/buttons that I want to keep open, like the help, print and show attachment buttons.  My validation framework uses it when a script validation fails at the subform level (that is cross field validation) to decide which field to give focus to.

I have also used them in my DoubleClick event cook book sample, http://cookbooks.adobe.com/livecycle_designer_es (I started moving my examples from this forum to the cook book as I think finding things in this forum is differcult).

I used to use JavaScript dynamic properties a lot, so something like "subFlags.Commercial = true", but I am now using the "Enforce strict scoping rules in JavaScript" as I have found my larger forms are a lot more stable, but this also means that JavaScript dynamic properties don't work.

I also like the values being typed, so you probably wouldn't have had the boolean to string conversion problem using this method.

Anyway, another way to skin the cat.

Bruce

Avatar

Level 10

No, that's brilliant, thanks Bruce. Wish I could get a brain-dump from you.

Avatar

Level 8

@BR001

I was looking for this code under XFAUtil... would you be able to provide a downloadable version of this script? Thanks.

Tarek