5 Replies Latest reply on May 7, 2010 6:31 AM by daslicht

    How to access form values from a form created via ActionScript

    SydWaters

      Hi,  I'm having trouble accessing the form values for a form that is created on the fly with Action Sc

      ript.

       

      I have a XML result that I terate through that generates several forms, one each inside a navigation container on an Accordian. I have this working fine, however I want to on a change event on any of the text inputs in one of these forms access all of the form items of the form that the text item belongs to and send this through as a post. The issue I'm having is on the change event for the text input I am having dififculty finding which form it belongs to and then iterateing over all of the form items. I have managed to get the form name as a string from the event but still not luck referncing the form.

       

      The blow code is an example, the first function parses the XML and generates the form, this works fine, however on the second function I would like to grab all of the formItems from the form that contains the one that text has changed on

       

      private function handleReturnSearchAccordian(event:ResultEvent):void{
          var XMLForm:XML = event.result as XML;
          var formCounter:uint = 0;
          for each (var property:XML in XMLForm.item){
           var entTypeName:String = property.ent_type_name;
           var entTypeId:uint = property.ent_type_id;
           var formHolder:NavigatorContent = new NavigatorContent();
           formHolder.label='Search '+entTypeName;
           var vGroup:VGroup = new VGroup();
           var form:Form = new Form();
           form.id='searchForm'+formCounter;
           form.name='searchForm'+formCounter;
          
           for each (var formProperty:XML in property.form_items.form_item){
            var inputType:String = String(formProperty.input_type);
            var inputLabel:String = String(formProperty.input_label);
            var inputId:String = String(formProperty.input_id);
         
            var formItem:FormItem = new FormItem();
            formItem.label = inputLabel;
            var textInput:TextInput = new TextInput();
            textInput.id=inputId;
            textInput.addEventListener(  flash.events.Event.CHANGE,handleChange);   
            formItem.addChild(textInput);
            form.addChild(formItem);
            }
           }
           vGroup.addElement(form);
           formHolder.addElement(vGroup);    
           searchAccordian.addChild(formHolder);
           formCounter++;
          }
         }
        
         private function handleChange(event:Event):void{
          var eventName:String = String(event.target);
          var eventExplode:Array = eventName.split('.');
          var formName:String = eventExplode[(eventExplode.length-3)];
         
         }

       

       

       

       

      If anyone can help it would be apprcaited, I maybe going about this in the totally wrong way, but the concept seems fine to me I just can't get it to work

        • 1. Re: How to access form values from a form created via ActionScript
          daslicht Level 2

          Heya,

          you write : 'send this through as a post.'

           

          Could you explain what exactly you are trying to do in a more simple way, please

          Why are you creating such forms programmatically, just curious.

           

          You could try to cast the ResultEvent as ArrayCollection and use this ArrayCollection as datasource for the Forms

           

          The form would look something like this:

           

           

          [Bindable] var ac:ArrayCollection = new ArrayCollection();
          
          private function handleReturnSearchAccordian(event:ResultEvent):void
          {
               ac = event.result as ArrayCollection;
          ...
          
          <mx:Form x="81" y="85">
               <mx:FormItem id='itemID1' label='yourLabel'>
                    <mx:TextInput id='textInput1' text='{ac[0].someData}' />
               </mx:FormItem>
          </mx:Form>
          
          here you can simply access data via its id's
          Probably that helps you in some way?
          

           

           

          Cheers

          Marc

          1 person found this helpful
          • 2. Re: How to access form values from a form created via ActionScript
            SydWaters Level 1

            Thanks for the reply Marc,

             

            In regards to 'send this through as a post.'.

             

            As I Iterate over all the formItems in the form, I will be stroing them in an ArrayColletion, and passing that to a HTTPService.send(arrayAcollection) which will "POST" to a php file and process the information from the form.

             

            The reason why I'm doing the forms with ActionScript is the forms get generated from data stored in a database, new forms and new fields can be added to the database at any time and this would be availble in the flex application straight awway with no recompiling.

             

            I had not thought of using casting the event to an Arraycollection. I will give that a go and see If I can work out how to get the all of the form items from a single formItem event.

             

            I will let ou know how I go.

            • 3. Re: How to access form values from a form created via ActionScript
              daslicht Level 2

              Hi,

              thank you for the insight, I wish you good luck !

              Cheers

              Marc

              • 4. Re: How to access form values from a form created via ActionScript
                SydWaters Level 1

                Hi Marc,

                 

                I have worked out a way that works for me, If I use the Dictionary object, I can save each form I create into that with the name as the key. Then when handling the change event on the text input I can get at the form from the dictionary object using the form name.

                 

                Cheers

                Luke

                • 5. Re: How to access form values from a form created via ActionScript
                  daslicht Level 2

                  Heya !

                  sounds good , thank you very much for reporting +

                   

                  Do you like to post a example maybe ?