Getting Started with Ext Forms

Tags: ExtJS
Word Count: 220
A reader had a couple of questions on Ext forms recently so I thought I would throw a quick tutorial together. This will by no means show you all of the features of the form classes but should give you a quick intro. The first thing people ask is why do I need forms in Ext, I can create them just fine using HTML. While this is true Ext makes it easy to submit the contents of the form using Ajax. When developers start taking a look at the form classes it really comes down to 3 things.
  • How do I create & customize forms
  • How do I submit the forms using AJAX
  • How can I load form data using AJAX
I am going to cover the first one in this post but I will follow it up with a posts on how to submit and load data in the near future. Creating forms is really not that hard once you know all the players. Your first assignment is head over to the Ext website where I found 2 very good articles. Tutorial : Getting Started with Forms
Loading & Submitting Data Now that we have a little introduction into forms lets start coding. The first thing we need to do is include our JavaScript and css files. For this example we will just include everything. Ext Includes
<!-- css -->
<link rel="stylesheet" type="text/css" href="../extjs/resources/css/ext-all.css" />
<!-- extjs -->   
<script type="text/javascript" src="../extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../extjs/ext-all.js"></script>
Create a Form Panel instance
<script type="text/javascript">
Ext.onReady(function() {
   var myForm = new Ext.form.FormPanel({
      
   });
});
</script>
While this does not do much this will get you started. Next we need to add some configuration to our form. The first thing to understand is that the FormPanel class extends Panel so many configuration options / methods / events come from the Panel class. Here are some of the main configuration options that we are going to use in our example.
  • id - The unique id of this component (defaults to an auto-assigned id)
  • renderTo - The id of the node, a DOM node or an existing Element that will be the container to render this component into.
  • title - The title text to display in the panel header
  • width - The width of this component in pixels
  • frame - True to render the panel with custom rounded borders, false to render with plain 1px square borders (defaults to false).
  • items - A single item, or an array of child Components to be added to this container. Each item can be any type of object based on Ext.Component.
  • buttons - An array of Ext.Button configs used to add buttons to the footer of this panel.
<script type="text/javascript">
Ext.onReady(function() {
   var myForm = new Ext.form.FormPanel({
      renderTo:"form1",
      title:"Basic Form",
      width:425,
      frame:true
   });
});
</script>
This really just sets up the container for our form. Now we to start adding form fields to our form. There are 2 ways to go about adding form items. We can add them in the constructor using the items attribute or we can use the add method from the FormPanel class. I actually prefer the inline method so thats how my example will look. Before we go adding fields we need to take a look at what type of fields we can add. Here are a list of different classes that we have access to.
  • Checkbox - Single checkbox field. Can be used as a direct replacement for traditional checkbox fields.
  • Combobox - A combobox control with support for autocomplete, remote-loading, paging and many other features.
  • DateField - Provides a date input field with a Ext.DatePicker dropdown and automatic date validation.
  • Field - Base class for form fields that provides default event handling, sizing, value handling and other functionality.
  • FieldSet - Standard container used for grouping form fields.
  • Hidden - A basic hidden field for storing hidden values in forms that need to be passed in the form submit.
  • HtmlEditor - Provides a lightweight HTML Editor component.
  • NumericField - Numeric text field that provides automatic keystroke filtering and numeric validation.
  • Radio - Single radio field. Same as Checkbox, but provided as a convenience for automatically setting the input type. Radio grouping is handled automatically by the browser if you give each radio in a group the same name.
  • TextArea - Multiline text field. Can be used as a direct replacement for traditional textarea fields, plus adds support for auto-sizing.
  • TextField - Basic text field. Can be used as a direct replacement for traditional text inputs, or as the base class for more sophisticated input controls (like Ext.form.TextArea and Ext.form.ComboBox).
  • TimeField - Provides a time input field with a time dropdown and automatic time validation.
  • Provides a convenient wrapper for TextFields that adds a clickable trigger button (looks like a combobox by default).
As I said before I prefer setting my items up inline so here is my final code that adds some form items to my form.
<script type="text/javascript">
Ext.onReady(function() {
   
   Ext.QuickTips.init();
   // message target    Ext.form.Field.prototype.msgTarget = "side";
      
   var myForm = new Ext.form.FormPanel({
      renderTo:"form1",
      title:"Basic Form",
      width:425,
      frame:true,
      items: [
         new Ext.form.TextField({
            id:"from",
            fieldLabel:"From",
            width:275,
            allowBlank:false,
            blankText:"Please enter a from address",
            vtype:"email",
            vtypeText:"The from field should be an email address in the format of user@domain.com"
         }),
         new Ext.form.TextField({
            id:"to",
            fieldLabel:"To",
            width:275,
            allowBlank:false,
            blankText:"Please enter a to address"
         }),
         new Ext.form.TextField({
            id:"subject",
            fieldLabel:"Subject",
            width:275,
            allowBlank:false,
            blankText:"Please enter a subject address"
         }),
         new Ext.form.TextArea({
            id:"message",
            fieldLabel:"Message",
            width:275,
            height:100
         })
      ],
      buttons: [
         {text:"Cancel"},
         {text:"Save"}
      ]
   });
});
</script>
A couple notes from the code above. The first is the allowBlank attribute is a way to make feilds required. If allowBlank is false then you should provide a blankText attribute to display a message when that field is left blank. The vType and vtypeText are ways to provide custom validation. If you look through the documentation you can learn more about that. Finally the msgTarget is where you are going to display the error messages. Valid options are "qtip,title,under,side,elementId". Here is what our form looks like. In the next article we will look at submitting the form data and actually doing something with it.

Comments

#1 Posted By: Chris Dawes Posted On: 2/27/08 6:46 PM
There's also a bunch of extensions that you can use to get custom types including:

- spinner (number spinner, date spinner, etc)
- multi select text field, multi select combo
- currency field
- multifield (custom field)
- date/time field
#2 Posted By: Chris Dawes Posted On: 2/27/08 6:47 PM
Oh the other cool thing is that you can have a formPanel which has a tab panel which has forms that acts like one form so you can tab out your forms like in cfform
#3 Posted By: Viktor Posted On: 3/19/08 9:52 AM
Nice and short one, I like it.
#4 Posted By: Sunshine Posted On: 4/21/08 3:34 AM
Hi I am new to Extjs. Can you show me the same loading and saving data back to DB in C# asp.net.
Thanks in advance
#5 Posted By: dbp Posted On: 6/16/08 6:34 AM
Is there any way I could use ext vtypes to do page level validation defining all required fields and validation constraints?
#6 Posted By: jbadfaxq Posted On: 6/17/08 11:00 PM
<a href="http://slmhwtxe.com">nsssdhap</a>; [URL=http://fxrsqyhw.com]trbafqxb[/URL] akyijezu http://cvdmfizq.com qktdvgmb epoesxyw
#7 Posted By: mirws Posted On: 6/20/08 12:32 AM
Hi...
very nice article.. :)
I'm a newbie in ajax Extjs, I got trouble when using datefield, because my application need a field that can handled date type and special characters (i.e. '*', '-', numeric and also 'h','m','s' ). If I use datefield, it can't store default value for my special characters, its only received date type. I have a plan to combine common textfield with datePicker. but until now I got stuck on it. Could you help me give a simple way to that thing.... thanks
#8 Posted By: wycliffe Posted On: 2/15/10 3:20 AM
thanks for the introduction,how do u load a form from a menu button i have the code of the form in a diffrent file -say the file is named formcol.js


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.