Getting Started with Ext Forms
Thursday February 14, 2008 9:00 AM
Word Count: 220
- How do I create & customize forms
- How do I submit the forms using AJAX
- How can I load form data using AJAX
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
<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>
<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.
Ext.onReady(function() {
var myForm = new Ext.form.FormPanel({
});
});
</script>
- 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.
Ext.onReady(function() {
var myForm = new Ext.form.FormPanel({
renderTo:"form1",
title:"Basic Form",
width:425,
frame:true
});
});
</script>
- 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).
<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.
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>
