Tuesday, October 21, 2008

How To Blank Out A Field's Value In InfoPath Without Using Managed Code

The other day I posted some guidance on how you can effectively fake a multi-select list in browser-enabled InfoPath forms, and even showed how to show or hide another control based upon a selection in that fake multi-select list.

The next level of problem is a tricky one... Let's say you have several options in a drop-down list box that you've embedded into a repeating table, just like I described in my October 20 post. Now, if the user's selections in the repeating table make any sections with controls appear (unhide) elsewhere on the form, and the user enters data into those newly unhidden controls, but THEN they go back and modify the selections in the repeating table such that the section with controls hides again, now what?

As you probably have already discovered, that data will NOT automatically get cleared out just because the section has hidden again. The guidance generally is that you would need to write managed code to remove that data from the .xml that your form is building behind the scenes.

But, in reality, you can use Rules to achieve the desired effect. It's not pretty, in fact it's quite tedious, but it can be done.

Trial and error will very likely ensue if you read the blog posts I read trying to find answers to how to do this... So here are some quick rules of thumb:

1) If you are trying to blank out a field outside of your repeating table based upon a value selected within your repeating table, you're going to want to set a rule at the point of the repeating table control itself.

However, it is important to note that when you attach a rule to a repeating table, it will only fire when the repeating table's structure changes, such as from the insertion or removal of a row. (In fact, I still get surprising results when deleting a row from the repeating table.)

When changes are made to the answers given within one or more of the controls on a row in that repeating table, nothing will happen, despite the existence of your seemingly valid rule. What you'll need to do to account for this is to create a corresponding rule attached to the control itself. The conditions will be the same and the actions will be the same as the rule you placed on the repeating table. The key distinction is that the dropdown list box control within a column in your repeating table has its own event that triggers its rules to fire separately from the repeating table's event, and you need to cover both of these events to get the best possible results out of your rules.

2) If you are trying to blank out a field not involving a repeating table based upon a change to a non-repeating value in your primary data source, you can just put the rule at the data source layer., but are probably better off putting it at the control for the non-repeating value. That way, there is clarity about what really triggers that rule to fire.

3) Take note. You are likely going to have to repeat very similar conditions and rules in a number of places. So far, I haven't found any way to use encapsulation to limit the amount of configuration of rules required to achieve the goal here, but if I do, I'll be sure to post it.

4) Test thoroughly. Rules can make for tricky form behavior, and you need to carefully consider all of the scenarios that can occur.

5 comments:

  1. Hello Jeff,
    Thank you for you post.
    If I am trying to blank out a field (in a repeationg table or any other control), I go to rules,set up condition, then choose "set up a fields value"action, choose a field and what do I type in the value section to make in blank?

    ReplyDelete
  2. Hi, Sabina. Thanks for the feedback. It's hard to believe it has been over two years since I wrote this post! I haven't tried it in a while, but if memory serves, you should be able to use an empty string i.e., "" with no space between the quotes. Let me know if that doesn't do the trick. Good luck and thanks for reading! - Jeff

    ReplyDelete
  3. Jeff, I also try to find your article "how you can effectively fake a multi-select list in browser-enabled InfoPath forms". Do you still have it available? It would make my form almost perfect :)

    ReplyDelete
  4. Check the one dated October 20, 2008:

    http://www.becraftsblog.com/2008/10/make-infopath-multi-select-control-that.html

    ReplyDelete
  5. Thank you Jeff.
    "" works, but it doesnt work for date picker...
    I wish there was a general easy way to reset data in hidden sections

    ReplyDelete

What do you think?