I had to build a dynamic form from a database not so long ago. There is
a whole lot more to it but I have boiled it down to the basic
functionality. The form component itself (MyForm.as) connects to the
database and grabs all the data to build the form dynamically. I have
not included any of that code.

This is how I did it:

main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute"
     xmlns:custom="CustomComponents.*">
     <custom:MyForm/>
</mx:Application>


CustomComponents.MyForm.as:
package CustomComponents
{
     import mx.containers.Form;
     import mx.containers.FormItem;
     import mx.controls.Alert;
     import flash.events.MouseEvent;
     import mx.controls.Button;
     import mx.controls.TextInput;
     import mx.controls.CheckBox;
     import mx.controls.TextArea;

     public class MyForm extends Form
     {
         public function MyForm()
         {
             super();
             var theFormItem:FormItem = new FormItem();
             var theTextInputItem:TextInput = new TextInput();

             theFormItem.setStyle("fontWeight", "bold");
             theTextInputItem.setStyle("fontWeight", "normal");
             theTextInputItem.id = "tester";
             theTextInputItem.width = 300;
             theFormItem.label = "tester";

             theFormItem.addChild(theTextInputItem);
             this.addChild(theFormItem);

             var submitButton:Button = new Button();
             submitButton.label = "Submit";
             submitButton.labelPlacement = "center";
             submitButton.addEventListener(MouseEvent.CLICK,
submitButtonClicked);
             this.addChild(submitButton);
         }

         private function submitButtonClicked(event:MouseEvent):void
         {
             var theFormFieldsString:String = "";
             var formChildren:Array = this.getChildren();
             for(var i:int = 0 ; i < formChildren.length ; i++)
             {
                 if(formChildren[i] is FormItem)
                 {
                     theFormFieldsString += (formChildren[i] as
FormItem).label + " ";
                     var formItemChildren:Array =
formChildren[i].getChildren();
                     if(formItemChildren[0] is TextInput)
                     {
                         theFormFieldsString += (formItemChildren[0] as
TextInput).text + "\n\n";
                     }
                     if(formItemChildren[0] is TextArea)
                     {
                         theFormFieldsString += (formItemChildren[0] as
TextArea).text + "\n\n";
                     }
                     if(formItemChildren[0] is CheckBox)
                     {
                         theFormFieldsString += (formItemChildren[0] as
CheckBox).selected ? "Yes" : "No";
                         theFormFieldsString += "\n\n";
                     }
                 }
             }
             Alert.show(theFormFieldsString);
         }
     }
}
--- In [email protected], "Jason B" <[EMAIL PROTECTED]> wrote:
>
>
> Yes your code works since its not dynamically building the form but
what
> happens if you try this code...it gets an error like i do
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
> layout="vertical" horizontalAlign="left" creationComplete="loaditem()"
>
> <mx:Script>
> <![CDATA[
> import mx.controls.Alert;
>      import mx.controls.TextInput;
>              import mx.managers.PopUpManager;
>              import mx.rpc.events.ResultEvent;
>              import mx.rpc.events.FaultEvent;
>              import mx.controls.Alert;
>              import mx.controls.ComboBox;
>              import mx.controls.Text;
>              import mx.controls.RadioButton;
>              import mx.controls.RadioButtonGroup;
>              import mx.controls.Label;
>              import mx.collections.ArrayCollection;
>              import mx.controls.dataGridClasses.DataGridColumn;
>              import mx.controls.TextArea;
>              import mx.events.*;
>              import mx.validators.*;
>              import mx.managers.ToolTipManager;
>              import mx.managers.IFocusManager;
>              import mx.managers.IFocusManagerComponent;
>              import mx.core.Container;
>              import mx.controls.Alert;
>              import mx.core.ComponentDescriptor;
>              import flash.utils.*;
>              import flash.net.navigateToURL;
>              import flash.net.URLRequest;
>              import flash.net.URLVariables;
>
> public function loaditem(){
>
>                          create_text = new TextInput();
>                          create_text.id = "tester";
>                          create_text.text =create_text.id;
>                          dataBox.addChild(create_text);
> }
>
> private function showText(evt:MouseEvent):void{
>
>
>
> var test:TextInput = this.getChildByName("tester") as TextInput;
> Alert.show(test.text);
> traceDisplayList(this);
> }
>
> public function traceDisplayList(container:DisplayObjectContainer,
> indentString:String = "->"):void{
>      var child:DisplayObject;
>      for (var i:uint=0; i < container.numChildren; i++)
>      {
>          child = container.getChildAt(i);
>          trace(indentString, child, child.name);
>          if (container.getChildAt(i) is DisplayObjectContainer)
>          {
>              traceDisplayList(DisplayObjectContainer(child), "    " +
> indentString)
>          }
>      }
> }
> ]]>
> </mx:Script>
> <mx:VBox id="dataBox"></mx:VBox>
>
> <mx:TextInput id="test1"/>
> <mx:Button label="Button" click="showText(event)"/>
> </mx:Application>
>
>
>
>
>
> --- In [email protected], Nik Derewianka nikstar@ wrote:
> >
> > That #1009 error means that it returned a null from the first line
> > because it couldn't find the child with that name.   getChildByName
is
> > not recursive so you need to be mindful of the nesting of your
> > requested object.
> >
> > The following app has the functionality working as you need it, but
it
> > also has a really handy listing function that displays all of the
> > current children recursively (outputs to the console window in Flex,
> > if you have the debug player) so that you can see the children that
> > are present and how they are nested.
> >
> > Regards,
> > Nik
> >
> > <?xml version="1.0" encoding="utf-8"?>
> > <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
> > layout="vertical" horizontalAlign="left">
> >  <mx:Script>
> >   <![CDATA[
> >    import mx.controls.Alert;
> >    private function showText(evt:MouseEvent):void{
> >     var test:TextInput = this.getChildByName("test" + 1) as
TextInput;
> >     Alert.show(test.text);
> >     traceDisplayList(this);
> >    }
> >
> >    public function
traceDisplayList(container:DisplayObjectContainer,
> > indentString:String = "->"):void{
> >        var child:DisplayObject;
> >        for (var i:uint=0; i < container.numChildren; i++)
> >        {
> >            child = container.getChildAt(i);
> >            trace(indentString, child, child.name);
> >            if (container.getChildAt(i) is DisplayObjectContainer)
> >            {
> >                traceDisplayList(DisplayObjectContainer(child), "   
"
> > + indentString)
> >            }
> >        }
> >    }
> >   ]]>
> >  </mx:Script>
> >
> >  <mx:TextInput id="test1"/>
> >  <mx:Button label="Button" click="showText(event)"/>
> > </mx:Application>
> >
>

Reply via email to