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:      23.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 value is existing. Used, e.g. for every property in a model record that is marked as  'required' ({@link M.Model#isRequired}.
 17  *
 18  * @extends M.Validator
 19  */
 20 M.PresenceValidator = M.Validator.extend(
 21 /** @scope M.PresenceValidator.prototype */ {
 22 
 23     /**
 24      * The type of this object.
 25      *
 26      * @type String
 27      */
 28     type: 'M.PresenceValidator',
 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      * @param {String} key
 36      * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false).
 37      */
 38     validate: function(obj, key) {
 39         if(obj.value === null || obj.value === undefined || obj.value === '') {
 40             if(obj.isView) {
 41                 this.validationErrors.push({
 42                     msg: this.msg ? this.msg : key + ' is required and is not set.',
 43                     viewId: obj.id,
 44                     validator: 'PRESENCE',
 45                     onSuccess: obj.onSuccess,
 46                     onError: obj.onError
 47                 });
 48             } else {
 49                 this.validationErrors.push({
 50                     msg: this.msg ? this.msg : obj.property + ' is required and is not set.',
 51                     modelId: obj.modelId,
 52                     property: obj.property,
 53                     validator: 'PRESENCE',
 54                     onSuccess: obj.onSuccess,
 55                     onError: obj.onError
 56                 });
 57             }
 58             return NO;
 59         }
 60         return YES;
 61     }
 62 
 63 });