Using Forms in Sencha Touch

Most apps that require user input make use of forms. In Sencha Touch forms are a wrapper around normal HTML5 forms, with additional options for validating and submitting data, and provide an easy way to lay out fields in an appealing visual style.

Editor Preview Open in Fiddle
Ext.application({
    name: 'Sencha',

    launch: function() {      
        var formPanel = Ext.create('Ext.form.Panel', {
            fullscreen: true,

            items: [{
                xtype: 'fieldset',
                items: [
                    {
                        xtype: 'textfield',
                        name : 'name',
                        label: 'Name'
                    },
                    {
                        xtype: 'emailfield',
                        name : 'email',
                        label: 'Email'
                    },
                    {
                        xtype: 'passwordfield',
                        name : 'password',
                        label: 'Password'
                    }
                ]
            }]
        });

        formPanel.add({
            xtype: 'toolbar',
            docked: 'bottom',
            layout: { pack: 'center' },
            items: [
                {
                    xtype: 'button',
                    text: 'Set Data',
                    handler: function() {
                        formPanel.setValues({
                            name: 'Seth',
                            email: '[email protected]',
                            password: 'secret'
                        });
                    }
                },
                {
                    xtype: 'button',
                    text: 'Get Data',
                    handler: function() {
                        Ext.Msg.alert('Form Values', JSON.stringify(formPanel.getValues(),null, 2));
                    }
                },
                {
                    xtype: 'button',
                    text: 'Clear Data',
                    handler: function() {
                        formPanel.reset();
                    }
                }
            ]
        });
    }
});

Creating a Form

The Form panel presents a set of form fields and provides convenient ways to load and save data. Usually a form panel contains the set of fields that you want to display, ordered inside the items configuration, as shown in the following example:

Editor Preview Open in Fiddle
Ext.application({
    name: 'Sencha',

    launch: function() {      
        Ext.create('Ext.form.Panel', {
            fullscreen: true,

            items: [
                {
                    xtype: 'textfield',
                    name : 'name',
                    label: 'Name'
                },
                {
                    xtype: 'emailfield',
                    name : 'email',
                    label: 'Email'
                },
                {
                    xtype: 'passwordfield',
                    name : 'password',
                    label: 'Password'
                }
            ]
        });
    }
});

In this example we created a simple form panel which could be used as a registration form to sign up to a service. We added a plain text for the user’s Name, an email and finally a password. In each case we provided a name config on the field for identifying it when we load and save data on the form.

Loading Data

Using the form we created above, we can load data into it in a different ways, the easiest way being the setValues method:

form.setValues({
    name: 'Seth',
    email: '[email protected]',
    password: 'secret'
});

You can also load model instances into a form - for example assuming that you have a User model and want to load a particular instance into the form:

Ext.define('MyApp.model.User', {
    extend: 'Ext.data.Model',
    config: {
        fields: ['name', 'email', 'password']
    }
});

var ed = Ext.create('MyApp.model.User', {
    name: 'Seth',
    email: '[email protected]',
    password: 'secret'
});

form.setRecord(ed);

Retrieving Form Data

Retrieving data from the form panel is usually done using the getValues method:

var values = form.getValues();

// values now looks like this:
{
    name: 'Seth',
    email: '[email protected]',
    password: 'secret'
}

It is also possible to listen to change events on individual fields in order to get more timely notification of changes that the user is making. In the following example we expand on the previous code containing the User model, and update the model as soon as any of the fields change:

var form = Ext.create('Ext.form.Panel', {
    listeners: {
        '> field': {
            change: function(field, newValue, oldValue) {
                ed.set(field.getName(), newValue);
            }
        }
    },
    items: [
        // as before
    ]
});

This example used a new capability of Sencha Touch, which enables you to specify listeners on child components of any container. In this case, we attached a listener to the change event of each form field that is a direct child of the form panel. The listener gets the name of the field that fired the change event, and updates the model instance with the new value. For example, changing the email field in the form will update the Model’s email field.

Submitting Forms

Sencha Touch provides a number of ways to submit form data. In the previous example we have a Model instance that we have updated, which enables us to use the Model’s save method to persist the changes to our server, without using a traditional form submission. Alternatively, we can send a normal browser form submit using the submit method:

form.submit({
    url: 'url/to/submit/to',
    method: 'POST',
    success: function() {
        alert('form submitted successfully!');
    }
});

In this case we provided the url to submit the form to inside the submit function call - alternatively you can set the url configuration when you create the form. You can specify other parameters (see submit for a full list), including callback functions for success and failure, which are called depending on whether the form submission was successful or not. These functions are usually used to take some action in your app after the data has been saved to the server.

Last updated