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:   Dominik
  6 // Date:      25.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 a given date. Validates whether it is possible to create a {@link M.Date} (then valid) or not (then invalid).
 18  *
 19  * @extends M.Validator
 20  */
 21 M.DateValidator = M.Validator.extend(
 22 /** @scope M.DateValidator.prototype */ {
 23 
 24     /**
 25      * The type of this object.
 26      *
 27      * @type String
 28      */
 29     type: 'M.DateValidator',
 30 
 31     /**
 32      * A RegEx describing a US date.
 33      * Used for validation.
 34      *
 35      * @type Function (actually a RegEx)
 36      */
 37     patternDateUS:  /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})(\s+([0-9]{2})\:([0-9]{2})(\:([0-9]{2}))?)?$/,
 38 
 39     /**
 40      * A RegEx describing a german date.
 41      * Used for validation.
 42      *
 43      * @type Function (actually a RegEx)
 44      */
 45     patternDateDE:  /^([0-9]{2})\.([0-9]{2})\.([0-9]{4})(\s+([0-9]{2})\:([0-9]{2})(\:([0-9]{2}))?)?$/,
 46 
 47     /**
 48      * 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.
 49      * Pushes different validation errors depending on where the validator is used: in the view or in the model.
 50      *
 51      * @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.
 52      * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false).
 53      */
 54     validate: function(obj, key) {
 55         /* validate the date to be a valid german or us date: dd.mm.yyyy or mm/dd/yyyy */
 56         if(obj.isView) {
 57             if(obj.value === null || obj.value === undefined || obj.value === '' || !(this.patternDateUS.test(obj.value) || this.patternDateDE.test(obj.value)) || !M.Date.create(obj.value)) {
 58                 var err = M.Error.extend({
 59                     msg: this.msg ? this.msg : key + ' is not a valid date.',
 60                     code: M.ERR_VALIDATION_DATE,
 61                     errObj: {
 62                         msg: this.msg ? this.msg : key + ' is not a valid date.',
 63                         viewId: obj.id,
 64                         validator: 'DATE',
 65                         onSuccess: obj.onSuccess,
 66                         onError: obj.onError
 67                     }
 68                });
 69                this.validationErrors.push(err);
 70                return NO;
 71             }
 72             return YES;
 73         } else {
 74             if(obj.value.type && obj.value.type !== 'M.Date' && (obj.value === null || obj.value === undefined || obj.value === '' || !M.Date.create(obj.value))) {
 75                 var err = M.Error.extend({
 76                     msg: this.msg ? this.msg : obj.property + ' is not a valid date.',
 77                     code: M.ERR_VALIDATION_DATE,
 78                     errObj: {
 79                         msg: this.msg ? this.msg : obj.property + ' is not a valid date.',
 80                         modelId: obj.modelId,
 81                         validator: 'DATE',
 82                         onSuccess: obj.onSuccess,
 83                         onError: obj.onError
 84                     }
 85                 });
 86                 this.validationErrors.push(err);
 87                 return NO;
 88             }
 89             return YES;
 90         }
 91     }
 92 });