1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved. 4 // Creator: Dominik 5 // Date: 25.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 a given date. Validates whether it is possible to create a {@link M.Date} (then valid) or not (then invalid). 17 * 18 * @extends M.Validator 19 */ 20 M.DateValidator = M.Validator.extend( 21 /** @scope M.DateValidator.prototype */ { 22 23 /** 24 * The type of this object. 25 * 26 * @type String 27 */ 28 type: 'M.DateValidator', 29 30 /** 31 * Validation method. First checks if value is not null, undefined or an empty string and then tries to create a {@link M.Date} with it. 32 * Pushes different validation errors depending on where the validator is used: in the view or in the model. 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, key) { 38 if(obj.value === null || obj.value === undefined || obj.value === '' || !M.Date.create(obj.value)) { 39 if(obj.isView) { 40 this.validationErrors.push({ 41 msg: this.msg ? this.msg : key + ' is not a valid date.', 42 viewId: obj.id, 43 validator: 'DATE', 44 onSuccess: obj.onSuccess, 45 onError: obj.onError 46 }); 47 } else { 48 this.validationErrors.push({ 49 msg: this.msg ? this.msg : obj.property + ' is not a valid date.', 50 modelId: obj.modelId, 51 property: obj.property, 52 validator: 'DATE', 53 onSuccess: obj.onSuccess, 54 onError: obj.onError 55 }); 56 } 57 return NO; 58 } 59 return YES; 60 } 61 62 });