      1 // ==========================================================================
      2 // Project:   The M-Project - Mobile HTML5 Application Framework
      3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
      4 // Creator:   Sebastian
      5 // Date:      22.11.2010
      6 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
      7 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
      8 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
      9 // ==========================================================================
     10
     11 m_require('core/datastore/validator.js')
     12
     13 /**
     14  * @class
     15  *
     16  * Validates if passed value is a number. Works with Strings and Numbers. Strings are parsed into numbers and then checked.
     17  *
     18  * @extends M.Validator
     19  */
     20 M.NumberValidator = M.Validator.extend(
     21 /** @scope M.NumberValidator.prototype */ {
     22
     23     /**
     24      * The type of this object.
     25      *
     26      * @type String
     27      */
     28     type: 'M.NumberValidator',
     29
     30     /**
     31      * Validation method. If value's type is not "number" but a string, the value is parsed into an integer or float and checked versus the string value with '=='.
     32      * The '==' operator makes an implicit conversion of the value. '===' would return false.
     33      *
     34      * @param {Object} obj Parameter object. Contains the value to be validated, the {@link M.ModelAttribute} object of the property and the model record's id.
     35      * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false).
     36      */
     37     validate: function(obj) {
     38         if(typeof(obj.value) === 'number') {
     39             return YES;
     40         }
     41
     42         /* == makes implicit conversion */
     43         if(typeof(obj.value) === 'string' && (parseInt(obj.value) == obj.value || parseFloat(obj.value) == obj.value)) {
     44             return YES;
     45         }
     46
     47         this.validationErrors.push({
     48             msg: obj.value + ' is not a number.',
     49             modelId: obj.modelId,
     50             property: obj.property,
     51             viewId: obj.viewId,
     52             validator: 'NUMBER',
     53             onSuccess: obj.onSuccess,
     54             onError: obj.onError
     55         });
     56         return NO;
     57     }
     58 });
     59