Flex loop over form values (mx:Form, mx:FormItems)
I’ve been messing around with the FlexOnRails Cairngorm/WebORB app and wanted to add some more fields to the flex form for projects (I’m building a construction project tracker so I need to add the project’s address). However as I’m a lazy programmer I quickly got tired of typing all the values to be passed in the event dispatcher. Here’s the original code:
private function projectFormValues () : Object
{
var values : Object = new Object();
values.street_address = street_address.text;
values.suite = suite.text;
values.city = city.text;
return values;
}
And below is my revised ‘projectFormValues’ method, it now loops over the form and gets all the values:
private function projectFormValues () : Object
{
var values : Object = new Object();
var formItems = project_form.getChildren();
for (var i = 0; i < formItems.length; i++)
{
var formItem = formItems[i].getChildren();
values[formItem[0].name] = formItem[0].text;
}
return values;
}
The form just for reference…
<mx:Form height="100%" width="100%" id="project_form">
<mx:FormItem label="Street Address:">
<mx:TextInput id="street_address" text="{model.selectedProject.street_address}"/>
</mx:FormItem>
<mx:FormItem label="Suite:">
<mx:TextInput id="suite" text="{model.selectedProject.suite}"/>
</mx:FormItem>
<mx:FormItem label="City:">
<mx:TextInput id="city" text="{model.selectedProject.city}"/>
</mx:FormItem>
</mx:Form>
This entry was written by
Alastair, posted on
December 21, 2006 at 11:59 am, filed under
ActionScript,
Flex. Bookmark the
permalink. Follow any comments here with the
RSS feed for this post.
or leave a trackback:
Trackback URL.
© Copyright 2006 - 2012 Alastair Dawson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 Comments
Very nice and DRY. It would be nice if the Form component had a method to do this.
Best,
Derek
Good job but I am getting errors.
1.) When no data is inserted into the form field
2.) How do I submit the entire object to a CFC for data posting!
Im Flex Builder 3 occurr error
1008: variable ‘formItem’ has no type declaration.
1008: variable ‘formItems’ has no type declaration.
1008: variable ‘i’ has no type declaration.
For i
i using
for( var i:int = 0; i < formItems.length; i++ ){
And this ok
How to resolve others ?
Please you Helpe me