Results 1 to 3 of 3

Thread: ST 2.1 - Custom form validation per field

  1. #1
    Sencha User
    Join Date
    Jan 2013
    Posts
    2
    Vote Rating
    0
      0  

    Default ST 2.1 - Custom form validation per field

    Hello,
    I've got 2 forms in my app:
    - Login form (email, password)
    - Registration fom (name, surname, email, password)

    I'm using model for validation:
    Code:
    Ext.define('First.model.User', {    
    extend : 'Ext.data.Model',
        config : {
            fields : [{
                name: 'id',
                type: 'integer'
            },
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'surname',
                type: 'string'
            },
            {
                name : 'email',
                type : 'string'
            }, {
                name : 'password',
                type : 'password'
            }],
            
            validations : [{
                type : 'presence',
                name : 'name',
                message : 'Enter name'
            },{
                type : 'presence',
                name : 'surname',
                message : 'Enter surname'
            },{
                type : 'presence',
                name : 'email',
                message : "Enter email"
            }, {
                type : 'presence',
                name : 'password',
                message : "Enter Password"
            }, {
                type : 'format',
                name : 'email',
                matcher : /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
                message : 'Email format is invalid'
            }]
        }
    });
    After submitting Login form I get an error concerning field that are not in the form (name, surname). Is it possible to rewrite presence rule so it won't be applied when there is no such field? Presence rule should be disabled in Login form, but not in Registration form.

    I've tried to make custom presence rule in Login form controller, but I've got no idea how to check field presence in form.

    Thanks for help in advance

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    No but you can choose to ignore the error you get back depending on how you are submitting and handling it. Not really a presence validation if you don't require the presence.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Sencha User
    Join Date
    Jan 2013
    Posts
    2
    Vote Rating
    0
      0  

    Default

    Application listens confirm button and after tapping it validates and submit the form (or shows errors). Handler for this action is placed in Controller. This is the handler:

    Code:
    // Checks form and log in    
    btn_confirmLogin : function() {
            
            var me = this;
            
            var model = Ext.ModelMgr.create(me.getForm_loginForm().getValues(), 'First.model.User');
    
            // Validate
            var errors = model.validate();
            var message = '';
    
            if (errors.isValid()) {
                // Validation successful - show loader
                Ext.Viewport.setMasked({
                    xtype : 'loadmask',
                    message : 'Logging in...'
                });
                // Login
                me.getForm_loginForm().submit({
                    url : 'http://localhost/first/?do=login',
                    method : 'POST',
                    success : function(form, result) {
                        // Save user
                        var user = Ext.create('First.model.User', {
                            id : result.user.id,
                            name : result.user.sFirstname,
                            surname : result.user.sLastname,
                            email : result.user.sEmail
                        });
    
                        // Save user to store
                        var users = Ext.create('First.store.Users');
                        // var users = Ext.getStore('usersStore');
                        users.add(user);
    
                        // Loader off
                        Ext.Viewport.setMasked(false);
    
                        // Move to homepage
                        me.push(me.getNvw_main(), 'First.view.HomePage');
                    },
                    failure : function(form, result) {
                        // Loader off
                        Ext.Viewport.setMasked(false);
                        
                        Ext.Msg.alert("Login Failed", result.error);
                    }
                });
    
            } else {
                // Validation failed
                var data = "";
                errors.each(function(item, index, length) {
                    data += item.getMessage() + '<br />';
                });
                Ext.Msg.alert("Validation Failed", data);
    
                return false;
            }
        }

Tags for this Thread

Posting Permissions

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