InfoPath Tip - Show text field if user selects other - SharePoint Development & Administration + InfoPath

Thursday, October 15, 2009

InfoPath Tip - Show text field if user selects other

InfoPath Basics - Display text box if user selects Other using conditional formatting

Promote two fields to a single SharePoint or Database column/field.

This article demonstrates how to configure an InfoPath form to display an optional text box for the user to enter a custom value if not available from the list of options. A separate field in the main data source of the InfoPath form will be used to store the value of the selected option, or the custom value if supplied by the user. Rules are set up to set the value of the separate field in the data source when an option is selected or when a custom value is entered into the text box.
When submitting the form, the text field with either the value selected, or the optional text input entered if the user selects other can be promoted to a single SharePoint column, or database table instead of promoting both the option field and the text field containing the "Other" value if selected.

Conditional formatting is also used to hide the text box until the user selects "Other" from the list of options.


Process:

Set up the Initial InfoPath Form

1. Design a new form template using InfoPath 2003 or 2007

2. From the Task Pane (CTRL+F1), open the Controls toolbar

3. Double click, or drag an Option Button control onto the default view of the form template. Enter the number of options required (remember to add an additional option for "Other").









Note: In this example, the Option Button will have 5 options including "Other".
The name of this field in the Main Data Source will be "Options"

4. Right Click the first Option and select "Option Button Properties..."



















5. Enter a Field Name for the field. Note: You only need to set the field name on one of the options for it to be applied to all.

6. Enter the value which will be used when this option is selected for the "Value when selected".

7. Press Ok

















8. Repeat Step 6 for each option. Enter "Other" as the value when selected for the final option.

9. Add a text field control to the form template (main data source) for a user to enter a custom value if required.








Note: In this example this field will be called "Other".

10. Add a separate text field to the form's Data Source only to store the selected value, or the value from the "Other" field by selecting "Add..." from the menu after right clicking the "myFields" group.













Note: For the purpose of this article, this field will be called "OptionSelected". This field does not need to be added to the form template. This field will contain the selected value from either the "Options" field or the custom value from the "Other" text field if required, and is explained further in the article.


Add Conditional Formatting to hide the "Other" text field when it is not required

11. Right Click on the "Other" text field control on the form template.
12. Select Conditional Formatting...
13. Press the "Add..." button to add a condition
14. Select the "Options" field created in step 3, "is not equal to" for the condition, and a text value of "Other" to test against (Select "Type text..." from the options).





15. Tick the "Hide this control" option for the formatting to be applied.









The "Other" text field will now only be visible when the user selects the "Other" option radio button.


Create Rules to set the value of the separate text field to be submitted with the form

15. Right click on any of the Radio Button options and select "Rules..."

16. Click "Add.." to create a new rule for this field.

17. Set the condition to the "Options" field, "is not equal to", Type "Other" as the value to test against.

18. Click the "Add Action..." button.

19. Select "Set a field's value" from the list of Actions

20. Select the "OptionSelected" field from the Main Data Source for the "Field" option

21. Select the "Options" field for the "Value" option.














This rule will now set the OptionSelected field to the value selected by the user if "Other" was not selected.

22. Right Click on the "Other" text field and select "Rules...", Click "Add..." to create a new rule for this field.

23. Set the condition for the rule to the "Options" field, "is equal to", and "Other" for the value.

24. Click the "Add Action..." button.

25. Set the Action to "Set field's value"

26. Set the "Field" option to the "OptionSelected" field

27. Set the "Value" option to the "Other" text field (current).














28. Right click on the OptionSelected field in the Main Data Source and set the default value to the default value of the "Options" field.















This will ensure that the OptionSelected field will always have a value in the case that a user submits the form using all default field values (rules would not have been applied as no changes we made to fields in the form's Data Source). If using the Options field as the default value as apposed to typing a specific default value, you will need to un-check the "Update this value when the result of the formula is recalculated" option to prevent the OptionSelected value being overwritten to equal the selected value of the Options field when an option is selected (rules are used to set the value of the OptionSelected field as explained above).

Conclusion

When a user selects one of the options available, but not the "Other" option, the value selected is set as the value for the separate text field created using rules. When the user selects "Other" from the options, the "Other" text field will become visible for the user to enter a custom value. When the user enters a custom value into the "Other" text box, rules are also used to set the selected value as the value for the separate text field in the main data source.

The separate field will always contain the value selected or manually entered by the user, which when promoted to a SharePoint or database column instead of promoting both the option button control and the "Other" text box. The separate field does not need to be added to the form template, as the value of the field will be set using rules when a user changes a value when completing the form.

Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

No comments:

Post a Comment