Search code examples
javascriptextjssencha-touch-2

How to get value of username and password in form Sencha Touch 2


I have a form with two fields that contain a username and password. I have a [loginbutton] with a handler. How do I get those value out of the fields in the handler of the the login button, for firing an event for the Controller?

                        handler: function() {
                            var form = this.parent.parent;
                            //HOW TO GET THESE VALUES?
                            var username = form.user;
                            var password = form.password;
                            form.fireEvent('login', username, password);

                        }

with a debugger i found this works, but is looks like a very complex solution to me:

               handler: function() {
                    var form = this.parent.parent;
                    var fieldset = form.items.items[0];
                    var username = fieldset.innerItems[0]._component._value;
                    var password = fieldset.innerItems[1]._component._value;
                    form.fireEvent('login', username, password);

This is code with config declaration.

Ext.define('AddressBook.view.Login', {
    title: "Luminizer Login",
    extend: 'Ext.form.Panel',
    xtype: 'loginForm',

    config: {
        url: 'login.php',
        standardSubmit: false,
        items: [
            {
                xtype: 'fieldset',
                title: 'MyCompony',
                instructions: 'Log in with username and password.',
                defaults: {
                    required: true,
                    labelAlign: 'left',
                    labelWidth: '40%'
                },
                items: [
                    {
                        //SCROLL DOWN FOR HANDLER
                        xtype: 'textfield',
                        name: 'name',
                        label: 'Name',
                        value: 'user',
                        autoCapitalize: false
                    },
                    {
                        xtype: 'passwordfield',
                        name: 'password',
                        label: 'Password',
                        value: 'test'
                    }
                ]
            },
            {
                xtype: 'toolbar',
                docked: 'bottom',
                items: [
                    {xtype: 'spacer'},
                    {
                        text: 'Reset',
                        handler: function() {
                            form.reset();
                        }
                    },
                    {
                        text: 'Login',
                        ui: 'confirm',
                        handler: function() {
                            var form = this.parent.parent;
                            //HOW TO GET THESE VALUES?
                            var username = form.user;
                            var password = form.password;
                            form.fireEvent('login', username, password);

                        }
                    }
                ]
            }
        ],
     ...
}

Solution

  • try this code

      var form = this.parent.parent;
      var name=form.getValues().firstname
    

    this code will gives you value of textfield which name is firstname.