[Component] CForm v1.0
January 2, 2008 by udayms
CForm is all about creating data entry screens. This component allows developers to create standardized forms/CRUD screens in their applications. The CForm component is a Data Entry component that can be very useful if -
- you are building business applications with many data entry/view screens
- you are working with a lot of developers. each handling separate screens/modules
- you wish all your screens to have the standard look & feel across the application
- you wish to avoid different developers/designers designing their own UI/UX paradigms for their specific screens
Let me try to explain this in a very simplified manner. CForm reads XML file to create user entry forms. To implement CForm, the following steps are involved.
- Developer creates XML file for each screen. The XML will contain the list of fields, controls to be used etc.
- CForm is called in an MXML.
- Now, the developer can use many of CForm’s exposed methods to access/manipulate data in the CForm.
The Demo walks you through a few use cases of how CForm can be used in real world. There are clear instructions on each example in the demo. For a detailed documentation on the XML part of this component, check out here and for the AS and MXML angle check out below. The source code will also be made available shortly in a 2 or 3 weeks. As of now, if you need the source code you will have to check it out from the SVN repository at http://flexedtoolkit.googlecode.com/. If you like this component and would be willing to extend a helping hand, send me a mail
.
If you have any questions/suggestions after viewing the demo, leave a comment on this page!!
**Update: Compiled SWC now available for download.
[ SWC | Demo | XML Documentation | Source of Example ]
CForm Properties
| Property | Access | Description |
| widgetsFile | Read-Write | The path of the xml file which contains the details of the controls |
CForm Methods
| Method | Description |
| clearContainer() | Clears the container of CForm. i.e. Removes the CForm from the container. |
| setData(obj:Object) | Sets the values of obj to the CForm fields. |
| getWidget(widgetId:String) | Returns the renderer of the widget in field. |
| createCFormFromXML(xml:XML) | creates CForm on the fly using the XML passed |
Events
| Event | Description |
| CFormLoadComplete | Fired when CForm completes loading all the fields |
Internals
CForm internally uses extended versions of the standard flex widgets. The extended widgets implements a common single interface - CFormItemRenderer.The extended widgets have some preset properties like height, width, stylename etc. These constant values are picked from DefaultConfig class.
Using CForm
MXML code
<flexed:CForm id=”cfrm”widgetsFile=”controls.xml” cFormValidation=”validateForm”verticalCenter=”0″ verticalScrollPolicy=”auto”
horizontalCenter=”0″ horizontalScrollPolicy=”off”
width=”100%” height=”100%”/>
AS Code
To set xml path and load CForm
str = “examples/cform/xmls/controls.xml”;
cfrm.widgetsFile = str;cfrm.init();
To get data from CForm
obj = cfrm.getData(); // to get only modified values
obj = cfrm.getData(false); // to get all values
To reset CForm
cfrm.resetCForm();
To set data to CForm
var oDataToSet:Object = new Object();
oDataToSet.fname = “udayms”;
oDataToSet.age = 27;
oDataToSet.gender = “male”;
cfrm.setData(oDataToSet);
To access one field in the CForm and access properties of the widget
cfrm.getWidget(“fname”).getUIComponent().addEventListener();
To create CForm on the fly using dynamic XML
cfrm.createCFormFromXML(new XML());
To Validate CForm using custom validation
// custom validation function
private function validateForm(id:String,
value:Object, values:Object):ValidationResult{
var result:ValidationResult = new ValidationResult(false);
if(id == “txtCoName”){
if(value == “” || value == null) result = new ValidationResult(true,null,“”,
“You HAVE to enter a company name!”);
}
return result; }
// do this to actually validate the
// CForm. Internally it
// will use validateForm() method.
cfrm.validateCForm();
I guess this covers most of the usecases. Anyway check out the demo. That should give pretty much a clear idea of what is possible with this component.




Nice! Coming from a live DOM, HTML form background, I’ve been frustrated by the fact that mxml forms are pre-compiled. I know that I can dynamically add elements with ActionScript but doing things declaratively (even if they are also dynamic) is cleaner, IMHO. I’d thought about building something like this but now you’ve done it, great!
How is this packaged? Do you load the standard Flex library and then your library?
How is it licensed?
@Larry: Hey…. Thanks for the comment. As I didn’t see any comments from the community I was beginnning to think that nobody wanted it. Anyway
, I guess I’ll just have to wait and see….. Anyway, as for your Qs:
Q: How is it licensed?
A: It’s open source. I learnt a lot from the community and this is how I can give back to the community. I’ll probably be releasing the code soon. Right now, its dirty and uncommented. thats why am holding back. In the mean while, you can checkout the entire code from the SVN repo of the google project link I have included in the post.
Q: How is this packaged? Do you load the standard Flex library and then your library?
Right now, It’s just a few folders with classes that you can add to your project and get working. I really want to compile it into a single SWC component or something, but have no clue how to do that!
This is really nice. I was also working on a project like this. I was trying to generate the UI in flex 2 dynamicly from XML. Maybe this is what i want to do. I have problem importing this project in flex. It says 1 “Type was not found or was not a compile-time constant: CForm.” Why? Is there any explanation of what Cform can do? Thanks.
[...] [Component] CForm v1.0 « Flexed - CForm is all about creating data entry screens. This component allows developers to create standardized forms/CRUD screens in their applications. The CForm component is a Data Entry component that can be very useful [...]
@Naum: It is not enough to download the source from the demo. You have to actually check out the code from the google code project. I will be posting the source code in a few days time in this blog till then the google project is where you can get the stuff.
Great job!… I’ve been checking the code and it’s really open to new extensions. You can add new “widgets” just by implementing the “CFormItemRenderer” class. Have you guys thought about including a new node name to cform to support “view states”? I think that could be a step beyond…
great job !
FileReference - A petition for Astro WE NEED YOUR VOTE !
Hi All! If We Want New Features in Flahs Player we will all need to VOTE!
I have submited it as a Feature Request. Follow the link and create a accont and vote for it!
http://bugs.adobe.com/jira/browse/SDK-14245
By the way we all need to get familiar with this “Adobe Bug System”
Please forward this message.
Cheers
Nice Job,
I’ve a few questions ?
1. is it possible for a group to span over several colums ?
2. is it possibel to place more fielditems in on a row in the same group ?
3. is there a reason why the button is missing as widget ?
@Sergio: Your Qs and my As -
Q. is it possible for a group to span over several colums ?
A: Not yet. But, thats in the roadmap.
Q: is it possibel to place more fielditems in on a row in the same group ?
A: Yes. Just add more than one formitem in a group.
Q: is there a reason why the button is missing as widget ?
A: Ok. This is something I spent sometime thinking about. The developer might want to position it differently for each view depending on the space availability. All the same, I have in the roadmap to allow developers to be able to add control points on top or bottom. I am looking at it more from a perspective of control points or action points. The developer may choose to have it as buttons, checkboxes, link buttons, image buttons or anything else.
i am having this message when i am trying to run the application can u help please
1046: Type was not found or was not a compile-time constant:
and where do we add the css files?
I guess you got this message when you downloaded the zip from the source of the demo and tried to run it. The reason is that the demo doesn’t have the source code of the CForm component. It has the source of only the demo. To get the source of the CForm component, you will have to check it out from the SVN repository of the google project which can be found at http://code.google.com/p/flexedtoolkit/source
ok 10x man but how can i download the source code should i copy and paste all mxml and as files into my flex ? i tried to do this i had these errors :
A file found in a source-path must have the same package structure ”, as the definition’s package, ‘flexed.widgets.form’. examples/src CFormItemCombobox.as
Can not resolve a multiname reference unambiguously. CFormItemRenderer (from C:\Documents and Settings\Administrator\My Documents\Flex Builder 3\examples\src\CFormItemRenderer.as) and flexed.widgets.form:CFormItemRenderer (from C:\Documents and Settings\Administrator\My Documents\Flex Builder 3\examples\src\CFormItemRenderer.as) are available. examples/src CFormItemDualText.as
can u give me some help please 10x again
Hi Udayms, my preferred FlexMan
Great new job (after CGrid….) with very nice skin.
One good little modified (were is possible) for example is:
# CFormItemText.as - Function:setAttributes(…
…….
…..
….
..
if(maxChars) textInput.maxChars=int(maxChars);
# relative XML:
thanks, max
Hi Udayms,
thx for the great component, works like a charm.
It took me a second to figure out that rows to the table widget can only be added through ActionScript
[e.g. mycform.getWidget("grdFriends").setValue(myFriendsCollection); ], since the (great) examples and the documentation didn’t cover that scenario.
Any plans on doing this from within the cform XML directly?
Anyway, really cool thing. I had to do the same in MFC C++ once, and you saved me the trouble of doing the same thing all over again in ActionScript. Let me know if you have ever need of my MFC library
cheers, Axel
Yes. You are right! We cannot add rows in the xml directly. we have to do it the way we you have written.
The way I see it… Rows in a table is ‘data’ or model. The xml file is for the view only not the model. Also, allowing the user to add rows to the table widget would make the xml really messy. Today, the only data that can be added to the xml are the values for comboxes etc. Even these are mostly enum values so can be considered part of the view.
Now, if we did do this in xml, then i feel we might be re-inventing the wheel. Validating and Parsing row information and creating a dataProvider might be really heavy and performance affecting.
These are the reasons, I kept rows out of the xml. Then again…these are just my views..
@ALL: Thanks for trying out and using CForm…. I guess its high time, I released the code in this blog. I am planning to get over my laziness and pack it up and put it on the blog sometime next week. Till then you will have to use the google project if you need the source code.
hi
great job
i try to build ( Flex 2.01 and Flex 3.0 ) but receive this error message :
[exec] C:\sviluppo\flex\flexedtoolkit\flexed-toolkit\src\flexed\widgets\grid\PrintGridContent.mxml(77): Error: Tipo non trovato o non Þ una costante della fase di compilazione: PrintGridHeader.
[exec]
[exec]
[exec]
[exec] C:\sviluppo\flex\flexedtoolkit\flexed-toolkit\src\flexed\widgets\grid\PrintGridContent.mxml(79): Error: Tipo non trovato o non Þ una costante della fase di compilazione: PrintGridFooter.
[exec]
[exec]
[exec]
[exec] C:\sviluppo\flex\flexedtoolkit\flexed-toolkit\src\flexed\widgets\grid\CGrid.mxml(179): Error: Accesso alla proprietÓ non definita Preferences.
[exec]
[exec] var colPreferences:Array = Preferences.getInstance.getGridPreference(this.id.toLowerCase());
[exec]
[exec] C:\sviluppo\flex\flexedtoolkit\flexed-toolkit\src\flexed\widgets\grid\CGrid.mxml(549): Error: Accesso alla proprietÓ non definita Preferences.
[exec]
[exec] Preferences.getInstance.setGridPreference(new String(this.id.toLowerCase()), dCols);
[exec]
@Gianfranco: You shouldn’t be getting this message. I dont really understand the language of the console output. But, I assume its kinds of a class not found error. I actually tried creating a new project and checked out file from the SVN. it came through without any issues. Can you recheck?
I’m very impressed with the form and have a question with regards to the radioButton Group. I’d like to bet able to vertically align each of the radio buttons instead of horizontally aligned. Can you give me any suggestions??
@graham: Actually i have deliberately kept out this feature. The radio button is laid out on the screen using Tile container. The tile container has a *direction* property. But as of today, we are not picking that value from the xml. Hence its not configurable from the xml. In short, if you want this done, you have to go to the CFormItemRadioButtonGroup class and in the constructor, you can see the line - container.direction = “horizontal”;
Change it to verical and you should be able to get what you want.
Cheers.
Thanks for that Udayms. I’ll have a look at that. I’ve also had another thought and was wondering if there was a way to filter the xml file?
What I’m thinking is, creating either a viewstack or using states to show only one form group at a time. It could also be used to reuse parts of an xml file for different situations.
This might be taking it one step too far but I thought it might be worth mentioning.
[...] riddled with forms. There is a task that I discovered a deep rooted hatred for. Laying out forms. CForm stabs this tedious task straight in the heart, allowing you to create forms on the fly using XML schema. The XML describes the form and the [...]
Great work! Loved the demo. I was looking for such a solution myself, so that we don’t have to define a view each time. Looking forward to your source code release. Thanks!
Fantastic app!
Extremely useful. Demo was really nice too. Just the thing I needed for a client!
Thanks!
OK… this is also something I am working on but you at this point are ahead of me.
1. Have you considered posting this on RiaForge.org?
2. In one of your demos (Advanced) the data changed in the right hand contacts grid didn’t post to the changed items if they were changed.
3. I am looking at generating both CF and Flex/AIR forms. My bigger question is does this generate the forms on the fly in Flex. (And if so, how do you tie it into an AS class to submit back to the server.)
Thanks for sharing.
John Farrar
@John Farrar:
1. Actually I tried to post this at RIAForge. But, for some wierd reason I am unable to login into the site.
2. Thats a bug.
Gotto fix it.
3. Yes. It creates forms on the fly. The way it works is, it creates the form based on an xml. You can pass this XML as an XML OBJECT or leave it in an xml file. When the form is being created, the component keeps track of what fields are being placed in the form. And then, we ask fo data back it goes through that returns the data - either all fields or only changed fields. If you check out the code from google project, you should be able understand how it works. Its quite simple. If you still run into doubts, leave me a mail. I shall send you a sample project with the cform on the front end and php in the back end. ok?
Hello
1st..congrats for this amazing project…
2nd. the XML documentation link isnt working
3rd. Is it possible to incorporate images (small gifs) to the forms? my forms are simply a list of images with a checkbox attached to each image. and the user should mark the images that fit him.
Thanks in advance
Afik
@Afik:
Oops…. Lemme check and fix that link…!!
As for your images requirement…. The way cform has been done is that only the most common form elements have been included by default. But, the component allows you to actually add new widgets very easily….
Btw, your specific requirement looks like a one-time need.. so, Why dont you just create a simple component with an image and a checkbox and then use a tile container to render the screen?
Hello
I am trying to update a cform with xml created by php script.
I’ve used HTTPService named userRequest1 and tied handleUR1 as its result event handler.
however I am getting this error:
1009: Cannot access a property or method of a null object reference.
at flexed.widgets.form::CForm/layoutCForm()[C:\workspace\builder3\cform\src\flexed\widgets\form\CForm.as:281]
at flexed.widgets.form::CForm/createCFormFromXML()[C:\workspace\builder3\cform\src\flexed\widgets\form\CForm.as:248]
at BigMedi/handleUR1()[E:\FlexWS\BigMedi\src\BigMedi.mxml:43]
at BigMedi/__userRequest1_result()[E:\FlexWS\BigMedi\src\BigMedi.mxml:71]
This is the code, what am i doing wrong? Is there is another way of doing this, maybe w/o the CreateCFormFromXML?
(the xml returned from the php is ok)
private function goProc():void{
var obj:Object = new Object();
obj = cfrm.getData(false);
var strData:String = ObjectUtil.toString(obj);
txtCfrm.text=strData;
cfrm1.init();
userRequest1.send();
}
private function handleUR1(event:ResultEvent):void
{ var x:XML;
x = event.result as XML;
cfrm1.clearContainer();
cfrm1.createCFormFromXML(x);
cfrm1.visible=true;
btnContinue.visible=true;
}
Can you post the xml here as well? or mail it to me?
Nice work!
I wondered how did you make left alignment for form item labels? Actually Flex won’t accept textAlign=”left” for form item labels. Could you please explain?
Thankyou
May God ‘flex’ you
@Giri: Am not using the form+formitem fr layout…. The CForm uses the grid layout. If you really look at it, you don’t get any specific advantage by using the form+formitem.
Hey guys, great component… the link to the XML Doc seems to be down, any other links we are get to this on?
Cheers
http://all_tiesto_here.ifastnet.com/
Uday, this looks great. I’m going to start a prototype project with it. It looks like your link to your “..detailed documentation on the XML part of this component” is missing it’s link.
Regards
Thankyou for posting this, Uday. I have this working with Flex on rails and it’s great. Is there any list of widgets? I want to use a slider for the age. Thanks!
Hi Uday,
great work!!!
just wanted to know what all widgets can we use,can we use a chart as well as you have already used datagrid.
or as johnny mentioned do you have list of widgets,clearly its form builder thats y it doesnt have the chart n other components - but would be great if we can extend it, if possible try to send out the source code to my mail as i am not able to use SVN and get it from googlecode - would be of great help!!
thanks a ton again
It creates forms on the fly. The way it works is, it creates the form based on an xml. You can pass this XML as an XML OBJECT or leave it in an xml file. When the form is being created, the component Extremely useful. Demo was really nice too. Just the thing I needed for a client!
Thanks, very interesting nice post.
I too was interested in seeing a list of widgets available. It’s not clear what’s available.
First let me join the applauding masses, nice work Uday!
Would you consider support for the TabNavigator control as an outer grouping facility through XML, that contains one cform per tab? I realize that this can be done through AS/mxml. E.g.
:
:
would result in a TabNavigator with two tabs containing the underlying forms.
Sorry, seems like my meta-code got zapped
cform label=”Setup”
cform label=”Properties”
where multiple cforms would automagically create cforms in a tab navigator.