Results 1 to 4 of 4

Thread: Get form for validation in a view from the controller

  1. #1
    Sencha User
    Join Date
    Nov 2011
    Posts
    21
    Answers
    2

    Default Answered: Get form for validation in a view from the controller

    Hi,
    In my model I've set up
    PHP Code:
    Ext.define('SA.model.User', {
        
    extend'Ext.data.Model',
        
    fields: ['id','uname''email'],
        
    validations: [{
            
    type'length',
            
    field'uname',
            
    min1
        
    }, {
            
    type'length',
            
    field'email',
            
    min1
        
    }]
    }); 
    so in my controller I've to check if data is valid
    how I can get the form from my controller

    PHP Code:
    Ext.define('SA.controller.Users', {
        
    extend'Ext.app.Controller',
        
    models: ['User'],
        
    stores: [
            
    'Users'
        
    ],
        
    views: [
            
    'user.List',
            
    'user.Edit',
            
    'user.Insert'
        
    ],
        
    init: function() {
            
    this.control({
                
    'userlist': {
                    
    itemdblclickthis.editUser,
                    
    itemclickthis.enableDeleteButton
                
    },
                
    'userlist > toolbar > button#btnAdd': {
                    
    clickthis.showInsertUser
                
    },
                
    'userinsert button[action=save]': {
                    
    clickthis.insertUser
                
    },
                
    'useredit button[action=save]': {
                    
    clickthis.updateUser
                
    }
            });
        },

       
        
    insertUser: function(button) {
          
    console.dir(this.getUserInsertView());
          
    // var win    = button.up('window'),
           //form   = win.down('form');
            //THIS DOESN'T WORK of course :)
            //if (form.isValid()) {
           //win.close();
           //alert('oii');
        
    }
        
    }); 
    My view
    PHP Code:
    Ext.define('SA.view.user.Insert', {
        
    extend'Ext.window.Window',
        
    alias 'widget.userinsert',

        
    title 'Insert User',
        
    layout'fit',
        
    autoShowtrue,

        
    initComponent: function() {
            
    this.items = [
                {
                    
    xtype'form',
                    
    items: [
                        {
                            
    xtype'textfield',
                            
    name 'uname',
                            
    fieldLabel'Name'
                        
    },
                        {
                            
    xtype'textfield',
                            
    name 'email',
                            
    fieldLabel'Email'
                        
    }
                    ]
                }
            ];

            
    this.buttons = [
                {
                    
    text'Save',
                    
    action'save'
                
    },
                {
                    
    text'Cancel',
                    
    scopethis,
                    
    handlerthis.close
                
    }
            ];

            
    this.callParent(arguments);
        }
    }); 

  2. In your Controller, right above your init() method, you can add a 'refs' array:

    Code:
     refs: [{
        ref: 'userInsert',
        selector: 'userinsert'
     }],
    In your Controller, you'll then 'auto-magically' have access to method this.getUserInsert(), which returns a reference to your userinsert widget. In your click handler method this.insertUser(), you can then do something like this:

    Code:
    var myForm = this.getUserInsert().down('form');
    if (!myForm.getForm().isValid()) {
        //complain to user.
    }

  3. #2
    Sencha User friend's Avatar
    Join Date
    Apr 2011
    Posts
    895
    Answers
    106

    Default

    In your Controller, right above your init() method, you can add a 'refs' array:

    Code:
     refs: [{
        ref: 'userInsert',
        selector: 'userinsert'
     }],
    In your Controller, you'll then 'auto-magically' have access to method this.getUserInsert(), which returns a reference to your userinsert widget. In your click handler method this.insertUser(), you can then do something like this:

    Code:
    var myForm = this.getUserInsert().down('form');
    if (!myForm.getForm().isValid()) {
        //complain to user.
    }
    Last edited by friend; 18 Jan 2012 at 4:52 AM. Reason: clarity

  4. #3
    Sencha User
    Join Date
    Nov 2011
    Posts
    21
    Answers
    2

    Default

    Thanks as usual for the reply.
    Now I've got the form but this
    PHP Code:
    myForm.getForm().isValid() 
    give me always true

  5. #4
    Sencha User
    Join Date
    Nov 2011
    Posts
    21
    Answers
    2

    Default

    Quote Originally Posted by whisher View Post
    Thanks as usual for the reply.
    Now I've got the form but this
    PHP Code:
    myForm.getForm().isValid() 
    give me always true
    I've worked it out with adding
    allowBlank: false
    in my form
    PHP Code:
    Ext.define('SA.view.user.Insert', {
        
    extend'Ext.window.Window',
        
    alias 'widget.userinsert',

        
    title 'Insert User',
        
    layout'fit',
        
    autoShowtrue,

        
    initComponent: function() {
            
    this.items = [
                {
                    
    xtype'form',
                    
    items: [
                        {
                            
    xtype'textfield',
                            
    name 'uname',
                            
    fieldLabel'Name',
                            
    allowBlankfalse
                        
    },
                        {
                            
    xtype'textfield',
                            
    name 'email',
                            
    fieldLabel'Email',
                            
    allowBlankfalse
                        
    }
                    ]
                }
            ];

            
    this.buttons = [
                {
                    
    text'Save',
                    
    action'save'
                
    },
                {
                    
    text'Cancel',
                    
    scopethis,
                    
    handlerthis.close
                
    }
            ];

            
    this.callParent(arguments);
        }
    }); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •