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