1. #1
    Sencha User
    Join Date
    Jan 2011
    Posts
    18
    Vote Rating
    1
    krike is on a distinguished road

      0  

    Question Answered: model validation will always pass, even when input is wrong

    Answered: model validation will always pass, even when input is wrong


    I'm a bit new to sencha touch but I have the following model:

    PHP Code:
    Ext.define("Contact", {        
        
    extend"Ext.data.Model",        
       
    fields: [            
          {
    name'inp_name',  type'string'},            
          {
    name'inp_email',  type'string'},            
          {
    name'txt_message',type'string'}        
       ],        
       
    validation: [            
          {
    type'presence',    name'inp_name'error'Name must be present'},            
          {
    type'length',         name'inp_name'min5},            
          {
    type'presence',    name'inp_email'error'Email must be present'},            
          {
    type'format',      name'inp_email'matcher: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/},            
          {
    type'presence',    name'txt_message'error'Message must be present'}        
       ]    
    }); 
    in the handler of a button I have the following:

    PHP Code:
    var data Ext.create('Contact', {                        
    inp_name'te',                        
    inp_email'christophedevartbe',                        
    txt_messagenull                    
    });
    var 
    errors data.validate();                    
    console.log('Is User valid?'errors.isValid()); //still returns true no matter what                    
    console.log('All Errors:'errors.items); //remains empty 
    But the problem is that, when the input does not match the validation it still returns true. Why does the validation still passes? Did I forgot something?

  2. Try this:

    Code:
    Ext.define("Contact", {
        extend: "Ext.data.Model",
        config : {
            fields: [
                {name: 'inp_name',  type: 'string'},
                {name: 'inp_email',  type: 'string'},
                {name: 'txt_message',type: 'string'}
            ],
            validations: [
                {type: 'presence',    name: 'inp_name', error: 'Name must be present'},
                {type: 'length',      name: 'inp_name', min: 5},
                {type: 'presence',    name: 'inp_email', error: 'Email must be present'},
                {type: 'format',      name: 'inp_email', matcher: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/},
                {type: 'presence',    name: 'txt_message', error: 'Message must be present'}
            ]
        }
    });
    
    Ext.setup({
        onReady : function() {
    
    var data = Ext.create('Contact', {
        inp_name: 'te',
        inp_email: 'christophedevartbe',
        txt_message: null
    });
    var errors = data.validate();
    
        }
    });

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    Answers
    3575
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Try this:

    Code:
    Ext.define("Contact", {
        extend: "Ext.data.Model",
        config : {
            fields: [
                {name: 'inp_name',  type: 'string'},
                {name: 'inp_email',  type: 'string'},
                {name: 'txt_message',type: 'string'}
            ],
            validations: [
                {type: 'presence',    name: 'inp_name', error: 'Name must be present'},
                {type: 'length',      name: 'inp_name', min: 5},
                {type: 'presence',    name: 'inp_email', error: 'Email must be present'},
                {type: 'format',      name: 'inp_email', matcher: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/},
                {type: 'presence',    name: 'txt_message', error: 'Message must be present'}
            ]
        }
    });
    
    Ext.setup({
        onReady : function() {
    
    var data = Ext.create('Contact', {
        inp_name: 'te',
        inp_email: 'christophedevartbe',
        txt_message: null
    });
    var errors = data.validate();
    
        }
    });
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Jan 2011
    Posts
    18
    Vote Rating
    1
    krike is on a distinguished road

      0  

    Default


    sorry I should have mentioned I'm already using the following:

    PHP Code:
    //load application
    Ext.application({
        
    name:    'Sencha',
        
        
    launch: function(){
            
    setupApp();
        }, 
    }); 
    and in the function setupApp() I have defined the model, created the form panel and tab panel and in the button handler I try to run the validation.
    PHP Code:
    {
                
    xtype'button',
                   
    text'Send',
                   
    ui'confirm',
                   
    handler: function() {
                        
    //Validate input
                        
    var data Ext.create('Contact', {
                            
    inp_name'te',
                            
    inp_email'christophedevartbe',
                            
    txt_messagenull
                        
    });

                        var 
    errors data.validate();
                        
    console.log('Is User valid?'errors.isValid()); //returns 'false' as there were validation errors
                        
    console.log('All Errors:'errors.items); //returns the array of all errors found on this model instance
                        
    if(errors.isValid())
                        {
                            
    alert('Form Validated');
                        }
                        else
                        {
                            
    alert('Form Not Validated');
                        }
                        return 
    false;
                        
    //send ajax request
                        
    Ext.Ajax.request({
                            
    url'contact.php',
                            
    method'post',
                            
    waitMsg'Submitting form',
                            
    //params: formBase.getForm().getValues(false),
                            
    success: function (responseopts) {
                                
    jsonResponse Ext.decode(response.responseText);  
                                if (
    jsonResponse.success == true) {
                                    
    Ext.Msg.alert('Success''We received your email and will come back to you asap.');
                                } else {
                                    
    Ext.Msg.alert('Error''We could not send your email, please very your input');
                                }
                            }
                        });
                    }
            } 

  5. #4
    Sencha User
    Join Date
    Nov 2011
    Posts
    53
    Vote Rating
    0
    Answers
    2
    paul_todd is on a distinguished road

      0  

    Default


    What looks like is wrong is that you have specified validation for the validations field object rather than "validations" - the "s" being important.

    I had a similar problem before with "extends" vs "extend"

  6. #5
    Sencha User
    Join Date
    Jan 2011
    Posts
    18
    Vote Rating
    1
    krike is on a distinguished road

      0  

    Default


    oh pleasssseeeeee ..... how could I miss that thank you.

  7. #6
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    Answers
    3575
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Quote Originally Posted by paul_todd View Post
    What looks like is wrong is that you have specified validation for the validations field object rather than "validations" - the "s" being important.

    I had a similar problem before with "extends" vs "extend"
    That and having things in the config object in PR4 and later.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  8. #7
    Sencha User
    Join Date
    Jan 2011
    Posts
    18
    Vote Rating
    1
    krike is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    That and having things in the config object in PR4 and later.
    sorry I don't understand what you mean.

  9. #8
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    Answers
    3575
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Quote Originally Posted by krike View Post
    sorry I don't understand what you mean.
    In PR4 or earlier a model definition should have the fields and such in the config object. Look at the code I posts, fields and validations is in the config object.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  10. #9
    Sencha User
    Join Date
    Jan 2011
    Posts
    18
    Vote Rating
    1
    krike is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    In PR4 or earlier a model definition should have the fields and such in the config object. Look at the code I posts, fields and validations is in the config object.
    ah yes I see. hence the console messages

  11. #10
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    Answers
    3575
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Quote Originally Posted by krike View Post
    ah yes I see. hence the console messages
    Every config now should go into the config object when using Ext.define throughout the ST2 framework now.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

Thread Participants: 2

Tags for this Thread