The following example shows how you can set the gap around the required indicator in a Flex form by setting the indicatorGap style, as seen in the following snippet:

<mx:Style>
    FormItem {
        indicatorGap: 24;
    }
</mx:Style>

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/06/setting-the-gap-around-the-required-indicator-in-a-flex-form/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Style>
        FormItem {
            backgroundColor: haloSilver;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import mx.events.SliderEvent;

            private var indicatorGapStyle:CSSStyleDeclaration;

            private function init():void {
                indicatorGapStyle = StyleManager.getStyleDeclaration("FormItem");
            }

            private function slider_change(evt:SliderEvent):void {
                indicatorGapStyle.setStyle("indicatorGap", evt.value);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="indicatorGap:" />
        <mx:HSlider id="slider"
                minimum="14"
                maximum="100"
                value="14"
                snapInterval="1"
                liveDragging="true"
                dataTipPlacement="bottom"
                dataTipPrecision="0"
                change="slider_change(event);" />
    </mx:ApplicationControlBar>

    <mx:Form>
        <mx:FormItem label="Lorem ipsum:">
            <mx:TextInput />
        </mx:FormItem>
        <mx:FormItem label="The quick brown fox jumped:" required="true">
            <mx:TextInput />
        </mx:FormItem>
        <mx:FormItem label="over the lazy dog:" required="true">
            <mx:Text text="The quick brown fox jumped over the lazy dog." />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Setting the gap around the required indicator in a Flex form

  1. Tim says:

    This is great. It takes care of the label area, but can it allow the textbox to expand. I would love to use this where the entry space is limited, but when a person has a really long name or text they could just stretch it out.

  2. fdk says:

    Hi, Can you help with required= true, the red star should stay with the text input and not move into the center as the indicator gap is increased or decreased.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree