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 it represents a minus number. Works with numbers and strings containing just a number.
 17  *
 18  * @extends M.Validator
 19  */
 20 M.NotMinusValidator = M.Validator.extend(
 21 /** @scope M.NotMinusValidator.prototype */ {
 22 
 23     /**
 24      * The type of this object.
 25      *
 26      * @type String
 27      */
 28     type: 'M.NotMinusValidator',
 29 
 30     /**
 31      * Validation method. Distinguishes between type of value: number or string. Both possible. If number value is checked if less than zero,
 32      * if string value is checked if ^prefixed with a minus sign ( - ).
 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 
 39        if(typeof(obj.value) === 'number') {
 40            if(obj.value < 0) {
 41                 this.validationErrors.push({
 42                     msg: obj.value + ' is a minus value. This is not allowed.',
 43                     modelId: obj.modelId,
 44                     property: obj.property,
 45                     viewId: obj.viewId,
 46                     validator: 'NUMBER',
 47                     onSuccess: obj.onSuccess,
 48                     onError: obj.onError
 49                 });
 50                 return NO;
 51            }
 52            return YES;
 53        }
 54 
 55        if(typeof(obj.value) === 'string') {
 56            var pattern = /-/;
 57            if(this.pattern.exec(obj.value)) {
 58                this.validationErrors.push({
 59                     msg: obj.value + ' is a minus value. This is not allowed.',
 60                     modelId: obj.modelId,
 61                     property: obj.property,
 62                     viewId: obj.viewId,
 63                     validator: 'NUMBER',
 64                     onSuccess: obj.onSuccess,
 65                     onError: obj.onError
 66                 });
 67                 return NO;
 68            }
 69            return YES;
 70        }
 71     }
 72 });