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 /** 12 * @class 13 * 14 * M.FormViews is the prototype of a form view, a container like view for grouping 15 * input views, e.g. M.TextFieldView. It covers a lot of the jobs concerning the 16 * validation of input views. There is no visible representation of an M.FormView, 17 * it is only used to ease the validation process and its accessing out of a 18 * controller. 19 * 20 * @extends M.View 21 */ 22 M.FormView = M.View.extend( 23 /** @scope M.FormView.prototype */ { 24 25 /** 26 * The type of this object. 27 * 28 * @type String 29 */ 30 type: 'M.FormView', 31 32 /** 33 * Determines whether to automatically show an alert dialog view out of the showError method 34 * if the validation failed or not. So if set to YES, all error messages are shown in an alert 35 * dialog view once the showError method is called. 36 * 37 * @type Boolean 38 */ 39 showAlertDialogOnError: YES, 40 41 /** 42 * The title of the alert view that comes up automatically if the validation fails, depending 43 * one the 'showAlertOnError' property. 44 * 45 * @type String 46 */ 47 alertTitle: 'Validation Error(s)', 48 49 /** 50 * This method triggers the validate() on all child views, respectively 51 * on their validators. 52 * 53 * @returns {Boolean} The result of the validation process: valid or not. 54 */ 55 validate: function() { 56 M.Validator.clearErrorBuffer(); 57 var isValid = YES; 58 if(this.childViews) { 59 var childViews = $.trim(this.childViews).split(' '); 60 for(var i in childViews) { 61 var childView = this[childViews[i]]; 62 if(childView && childView.validators) { 63 _.each(childView.validators, function(validator) { 64 if(!validator.validate(childView, childViews[i])) { 65 isValid = NO; 66 } 67 }); 68 } 69 if(childView && childView.cssClassOnError) { 70 childView.removeCssClass(childView.cssClassOnError); 71 } 72 } 73 } 74 75 return isValid; 76 }, 77 78 /** 79 * This method adds a css class specified by the cssClassOnError property to any 80 * view that caused a validation error and has this property specified. 81 * 82 * If the showAlertDialogOnError property is set to YES, a alert dialog view 83 * is display additionally, presenting the error messages of all invalid views. 84 */ 85 showErrors: function() { 86 var errors = ''; 87 _.each(M.Validator.validationErrors, function(error) { 88 var view = M.ViewManager.getViewById(error.viewId); 89 if(view && view.cssClassOnError) { 90 view.addCssClass(view.cssClassOnError); 91 } 92 errors += '<li>' + error.msg + '</li>'; 93 }); 94 95 if(this.showAlertDialogOnError) { 96 M.DialogView.alert({ 97 title: this.alertTitle, 98 message: errors 99 }); 100 } 101 } 102 103 });