Setting the help content on a Spark FormItem container in Flex Hero

The following example shows how you can set the help content on a Spark FormItem container in Flex Hero by setting the helpContent property.

The following example(s) require Flash Player 10 and the beta Adobe Flex Hero SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex Hero SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Hero.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/08/29/setting-the-help-content-on-a-spark-formitem-container-in-flex-hero/ -->
<s:Application name="Spark_FormItem_helpContent_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <s:Form id="frm" defaultButton="{submitBtn}">
        <s:FormHeading label="Spark Form Heading" />
        <s:FormItem sequenceLabel="1." label="Username:">
            <s:helpContent>
                <s:Label text="user@domain.com" />
            </s:helpContent>
            <s:TextInput id="username" />
        </s:FormItem>
        <s:FormItem sequenceLabel="2." label="Password:">
            <s:helpContent>
                <s:Label text="weak" color="red" />
            </s:helpContent>
            <s:TextInput id="password1" displayAsPassword="true" />
        </s:FormItem>
        <s:FormItem sequenceLabel="3." label="Confirm password:">
            <s:helpContent>
                <s:Label text="One" color="red" />
                <s:Label text="Two" color="blue" />
            </s:helpContent>
            <s:TextInput id="password2" displayAsPassword="true" />
        </s:FormItem>
        <s:FormItem>
            <s:layout>
                <s:HorizontalLayout />
            </s:layout>
            <s:Button id="resetBtn" label="Reset" />
            <s:Button id="submitBtn" label="Submit" />
        </s:FormItem>
    </s:Form>
 
</s:Application>

As you can see from the third FormItem container, the help contents are arranged in a basic Group container (hence the overlapping of the two labels). If you have multiple items in your helpContent group, you can wrap them in something like an HGroup or VGroup, as seen in the following snippet:

<s:FormItem sequenceLabel="3." label="Confirm password:">
    <s:helpContent>
        <s:HGroup>
            <s:Label text="One" color="red" />
            <s:Label text="Two" color="blue" />
        </s:HGroup>
    </s:helpContent>
    <s:TextInput id="password2" displayAsPassword="true" />
</s:FormItem>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/08/29/setting-the-help-content-on-a-spark-formitem-container-in-flex-hero/ -->
<s:Application name="Spark_FormItem_helpContent_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        initialize="init();">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            import spark.components.Button;
            import spark.components.Form;
            import spark.components.FormHeading;
            import spark.components.FormItem;
            import spark.components.HGroup;
            import spark.components.Label;
            import spark.components.TextInput;
            import spark.layouts.HorizontalLayout;
 
            protected var frm:Form;
            protected var username:TextInput;
            protected var password1:TextInput;
            protected var password2:TextInput;
            protected var resetBtn:Button;
            protected var submitBtn:Button;
 
            protected function init():void {
                username = new TextInput();
 
                password1 = new TextInput();
                password1.displayAsPassword = true;
 
                password2 = new TextInput();
                password2.displayAsPassword = true;
 
                resetBtn = new Button();
                resetBtn.label = "Reset";
 
                submitBtn = new Button();
                submitBtn.label = "Submit";
 
                var fh:FormHeading = new FormHeading();
                fh.label = "Spark Form Heading";
 
                var lbl1:Label = new Label();
                lbl1.text = "user@domain.com";
 
                var fi1:FormItem = new FormItem();
                fi1.sequenceLabel = "1.";
                fi1.label = "Username:";
                fi1.helpContent = [lbl1];
                fi1.addElement(username);
 
                var lbl2:Label = new Label();
                lbl2.text = "weak";
                lbl2.setStyle("color", "red");
 
                var fi2:FormItem = new FormItem();
                fi2.sequenceLabel = "2.";
                fi2.label = "Password:";
                fi2.helpContent = [lbl2];
                fi2.addElement(password1);
 
                var lbl3:Label = new Label();
                lbl3.text = "One";
                lbl3.setStyle("color", "red");
 
                var lbl4:Label = new Label();
                lbl4.text = "Two";
                lbl4.setStyle("color", "blue");
 
                var helpConGr:HGroup = new HGroup();
                helpConGr.addElement(lbl3);
                helpConGr.addElement(lbl4);
 
                var fi3:FormItem = new FormItem();
                fi3.sequenceLabel = "3.";
                fi3.label = "Confirm password:";
                fi3.helpContent = [helpConGr];
                fi3.addElement(password2);
 
                var fi4:FormItem = new FormItem();
                fi4.layout = new HorizontalLayout();
                fi4.addElement(resetBtn);
                fi4.addElement(submitBtn);
 
                frm = new Form();
                frm.defaultButton = submitBtn
                frm.addElement(fh);
                frm.addElement(fi1);
                frm.addElement(fi2);
                frm.addElement(fi3);
                frm.addElement(fi4);
                addElement(frm);
            }
        ]]>
    </fx:Script>
 
</s:Application>

This entry is based on a beta version of the Flex Hero SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Hero SDK.