Success! Looks like we've fixed this one. According to our records the fix was applied for EXTJS-3325 in a recent build.
  1. #1
    Ext JS Premium Member meditecsports's Avatar
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    47
    Vote Rating
    2
    meditecsports is on a distinguished road

      0  

    Default Ext.data.validations presence function return false for numerfields with "0" values

    Ext.data.validations presence function return false for numerfields with "0" values


    Browsers: Firefox 4.0 and Google Chrome 10.0.648.127.
    ExtJs version 4.0.2a.

    There is a problem with Ext.data.Model validation type = "presence" when using numberfield. In this case it validation for value "0" is false.

    For example this code:
    PHP Code:
    Ext.define('Number', {
                    
    extend'Ext.data.Model',
                    
    fields: [
                        {
    name'number'type'int'}
                    ],
                    
    validations: [
                        {
    type'presence'field'number'}
                    ]
                });

                var 
    instance Ext.ModelManager.create({
                    
    number0
                
    }, 'Number');

                var 
    errors instance.validate();
                
    errors.isValid(); //false
                
    errors.getByField('number'); // [{field: 'number',  message: 'must be present'}] 
    I think presence function does not count cases of int type fields validation.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,327
    Vote Rating
    850
    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


    Thank you for the bug report. Please note that we have a new bug template and would appreciate if you could use this from now on.
    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.

  3. #3
    Sencha User
    Join Date
    Mar 2011
    Location
    Germany
    Posts
    198
    Vote Rating
    1
    Nickname is on a distinguished road

      0  

    Default


    Just ran into the same problem



    REQUIRED INFORMATION Ext version tested:
    • Ext 4.0.2a
    Browser versions tested against:
    • FF4 (firebug 1.7 installed)
    Description:
    • Presence Validator fails if value is zero
    Steps to reproduce the problem:
    • Define a model validator with presence validator for a field
    • Field value to validate is (int) 0
    The result that was expected:
    • The validator should return "true" because zero can be a wanted value for a field (like numberfield)
    The result that occurs instead:
    • Validator returns "false"
    Test Case: (from initial reporter meditecsports)
    Code:
     Ext.define('Number', {
        extend: 'Ext.data.Model',
    fields: [
    {name: 'number', type: 'int'}
    ],
        validations: [
    {type: 'presence', field: 'number'}
        ]
    });
    
    var instance = Ext.ModelManager.create({
        number: 0
    }, 'Number');
    
    var errors = instance.validate();
    errors.isValid(); //false
    errors.getByField('number'); // [{field: 'number',  message: 'must be present'}]  
    HELPFUL INFORMATION Debugging already done:
    • in Ext.data.validations, the method "presence" is using "!!value", which is false if value is 0
    • example: !!0 === false
    Possible fix:
    • in Ext.data.validations.presence():
    • maybe "return !Ext.isEmpty(value)" ?!
      /**
      * Validates that the given value is present
      * @param {Object} config Optional config object
      * @param {Mixed} value The value to validate
      * @return {Boolean} True if validation passed
      */
      presence: function(config, value) {
      if (value === undefined) {
      value = config;
      }

      return !Ext.isEmpty(value);
      },
    Operating System:
    • Ubuntu 10.10

    //edit: Oh, there is already a bug id for this, did not see that

    //edit2: if you don't want to change the Ext source, you can use a format validator as a temporary fix
    PHP Code:
    validations: [
            {
    type'format'field'number'matcher: /^\d+$/}
        ] 

  4. #4
    Ext JS Premium Member zodeus's Avatar
    Join Date
    Jan 2010
    Location
    Vancouver Canada
    Posts
    36
    Vote Rating
    0
    zodeus is on a distinguished road

      0  

    Default


    I can confirm this is an issue in 4.0.4, the presence method for what ever reason is return the value of the field rather than a true so if the field is an int with a zero or a boolean value of false it will always fail. Looking at the other validation methods they are returning either true or false, however the presence method returns either the value on success or the config on failure which seems inconsistent with the other validation methods.

  5. #5
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    9
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    Thanks, this is now fixed in the 4.1.0 branch
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer