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:      23.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 value is existing. Used, e.g. for every property in a model record that is marked as  'required' ({@link M.Model#isRequired}.
 18  *
 19  * @extends M.Validator
 20  */
 21 M.PresenceValidator = M.Validator.extend(
 22 /** @scope M.PresenceValidator.prototype */ {
 23 
 24     /**
 25      * The type of this object.
 26      *
 27      * @type String
 28      */
 29     type: 'M.PresenceValidator',
 30     
 31     /**
 32      * 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.
 33      * Pushes different validation errors depending on where the validator is used: in the view or in the model.
 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      * @param {String} key
 37      * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false).
 38      */
 39     validate: function(obj, key) {
 40         if(obj.value === null || obj.value === undefined || obj.value === '') {
 41             if(obj.isView) {
 42 
 43                 var err = M.Error.extend({
 44                     msg: this.msg ? this.msg : key + ' is required and is not set.',
 45                     code: M.ERR_VALIDATION_PRESENCE,
 46                     errObj: {
 47                         msg: this.msg ? this.msg : key + ' is required and is not set.',
 48                         viewId: obj.id,
 49                         validator: 'PRESENCE',
 50                         onSuccess: obj.onSuccess,
 51                         onError: obj.onError
 52                     }
 53                 });
 54                 this.validationErrors.push(err);
 55                 
 56             } else {
 57                 var err = M.Error.extend({
 58                     msg: this.msg ? this.msg : obj.property + 'is required and is not set.',
 59                     code: M.ERR_VALIDATION_PRESENCE,
 60                     errObj: {
 61                         msg: this.msg ? this.msg : obj.property + ' is required and is not set.',
 62                         modelId: obj.modelId,
 63                         property: obj.property,
 64                         validator: 'PRESENCE',
 65                         onSuccess: obj.onSuccess,
 66                         onError: obj.onError
 67                     }
 68                 });
 69                 this.validationErrors.push(err);
 70             }
 71             return NO;
 72         }
 73         return YES;
 74     }
 75 
 76 });