1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved. 4 // (c) 2011 panacoda GmbH. All rights reserved. 5 // Creator: Sebastian 6 // Date: 22.11.2010 7 // License: Dual licensed under the MIT or GPL Version 2 licenses. 8 // http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE 9 // http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE 10 // ========================================================================== 11 12 m_require('core/datastore/validator.js') 13 14 /** 15 * @class 16 * 17 * Validates if it represents a minus number. Works with numbers and strings containing just a number. 18 * 19 * @extends M.Validator 20 */ 21 M.NotMinusValidator = M.Validator.extend( 22 /** @scope M.NotMinusValidator.prototype */ { 23 24 /** 25 * The type of this object. 26 * 27 * @type String 28 */ 29 type: 'M.NotMinusValidator', 30 31 /** 32 * Validation method. Distinguishes between type of value: number or string. Both possible. If number value is checked if less than zero, 33 * if string value is checked if ^prefixed with a minus sign ( - ). 34 * 35 * @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. 36 * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false). 37 */ 38 validate: function(obj) { 39 40 if(typeof(obj.value) === 'number') { 41 if(obj.value < 0) { 42 var err = M.Error.extend({ 43 msg: this.msg ? this.msg : obj.value + ' is a minus value. This is not allowed.', 44 code: M.ERR_VALIDATION_NOTMINUS, 45 errObj: { 46 msg: obj.value + ' is a minus value. This is not allowed.', 47 modelId: obj.modelId, 48 property: obj.property, 49 viewId: obj.viewId, 50 validator: 'NUMBER', 51 onSuccess: obj.onSuccess, 52 onError: obj.onError 53 } 54 }); 55 this.validationErrors.push(err); 56 return NO; 57 } 58 return YES; 59 } 60 61 if(typeof(obj.value) === 'string') { 62 var pattern = /-/; 63 if(this.pattern.exec(obj.value)) { 64 var err = M.Error.extend({ 65 msg: this.msg ? this.msg : obj.value + ' is a minus value. This is not allowed.', 66 code: M.ERR_VALIDATION_NOTMINUS, 67 errObj: { 68 msg: obj.value + ' is a minus value. This is not allowed.', 69 modelId: obj.modelId, 70 property: obj.property, 71 viewId: obj.viewId, 72 validator: 'NUMBER', 73 onSuccess: obj.onSuccess, 74 onError: obj.onError 75 } 76 }); 77 this.validationErrors.push(err); 78 return NO; 79 } 80 return YES; 81 } 82 } 83 });