Sunday, May 8, 2011

Add a linebreak in rules in InfoPath

Applys to: InfoPath Difficulty: 4

If you have been working with rules in InfoPath, you've probably also stumbled upon the limitation to rules, that you cannot create a linebreak.

Lets say you want a user to type in some mail information. And since you want to validate the user input, you need 3 or 4 separate fields (at least now, for testing purposes). The 4 fields would be First name, Last name, Address and Zip code / city.

For this tip, I will just post the combined text into another text field. From there, you should have an idea on how to get on.

Now. The first thing you need to do, is to open a text editor like notepad. Then you type in the following text, and save it somewhere on your pc as "linebreak.xml":


<?xml version="1.0" encoding="UTF-8"?>
<characters 
    cr="&#xD;" 
    lf="&#xA;" 
    crlf="&#xD;&#xA;" 
/>

I've created this form, with the fields from before, a button to initiate the action of combining the text, and a field to contain the altered text:

To get the linebreak to work, we need to import the XML file we created before. To do this, you go to "Tools" -> "Data Connections" and click "Add...".

You want to receive data, so fill out the window as shown below, and click "Next":


Choose "XML document" and click "Next":

Click "Browse..." and locate the "linebreak.xml" file you created earlier and click "Next":


You want to include the file in the form template, so leave the selection and click "Next":

If you want to, you can choose another name for the data connection. The name is only used to identify the data connection, but will have no other impact on the usage.

Now the linebreak function has been imported, and you're ready to put it to use. However, we first need to make sure, that the text field the should contain the end result, can handle multi-line text.
To do this, simply double-click the field, click on the "Display" pane and mark the "Multi-line" option.

The last step, is to create the rule that will create our text, so go ahead and add a rule to the button. I've used the concat() function, to combine the fields, and linebreaks:

The linebreaks are added like any other field, but to find them, you need to choose the data source name you typed in earlier, in my case, "linebreak":

Just select "crlf" and click "OK".

If you've created the rule as shown on the above images, you should be able to get the following result, by filling out the 4 fields above the button in preview-mode and then clicking the button afterwards:


This concludes this tip.

No comments:

Post a Comment