Changeset 231

Show
Ignore:
Timestamp:
05/10/07 15:55:13 (6 years ago)
Author:
stefanp
Message:

renamed get/setDataObj to get/setDataObject

renamed submitCaption to submitValue

changed jala.Form.createComponents to use a stripped down label text if no name is provided for a component.

The constructor of a component now throws an error if no argument is provided. jala.Form.create writes that error to the log.

renamed jala.Form.REQUIRED to jala.Form.REQUIRE

Location:
jala/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • jala/trunk/code/Form.js

    r230 r231  
    7575    * @see #save 
    7676    */ 
    77    this.setDataObj = function(newDataObj) { 
     77   this.setDataObject = function(newDataObj) { 
    7878      dataObj = newDataObj; 
    7979      return; 
     
    8585    * @returns The data object of this jala.Form instance 
    8686    */ 
    87    this.getDataObj = function() { 
     87   this.getDataObject = function() { 
    8888      return dataObj; 
    8989   }; 
     
    165165 
    166166   // init private fields: 
    167    var submitCaption, errorMessage = undefined; 
    168  
    169    /** 
    170     * Returns the caption of the submit button. 
    171     * @returns caption 
     167   var submitValue, errorMessage = undefined; 
     168 
     169   /** 
     170    * Returns the value (ie. text) of the submit button. 
    172171    * @type String 
    173172    */ 
    174    this.getSubmitCaption = function() { 
    175       return submitCaption; 
     173   this.getSubmitValue = function() { 
     174      return submitValue; 
    176175   }; 
    177176    
    178177   /** 
    179     * Sets the caption of the submit button. 
    180     * @param {String} newCaption 
    181     */ 
    182    this.setSubmitCaption = function(newCaption) { 
    183       submitCaption = newCaption; 
     178    * Sets the value (ie. text) of the submit button. 
     179    * @param {String} newSubmitValue 
     180    */ 
     181   this.setSubmitValue = function(newSubmitValue) { 
     182      submitValue = newSubmitValue; 
    184183      return; 
    185184   }; 
     
    271270      form.setLegend(config.legend); 
    272271   } 
    273    if (config.submitCaption) { 
    274       form.setSubmitCaption(config.submitCaption); 
     272   if (config.submitValue) { 
     273      form.setSubmitValue(config.submitValue); 
    275274   } 
    276275   if (config.errorMessage) { 
     
    299298      var constr = jala.Form.Component[clazzName]; 
    300299      if (!constr) { 
    301          // invalid constructor 
     300         // invalid constructor: 
    302301         var logStr = "jala.Form encountered unknown component type " + element["type"] + " in config of form "; 
    303302         logStr += (container.form) ? container.form.name : container.name; 
     
    305304         continue; 
    306305      } 
    307       var component = new constr(element.name); 
     306      var name = element.name; 
     307      if (!name && element.label) { 
     308         name = element.label.toAlphanumeric().toLowerCase(); 
     309      } else if (!name && constr == jala.Form.Component.Fieldset) { 
     310         var str = "fieldset"; 
     311         while(container.components[str]) { 
     312            str += "1"; 
     313         } 
     314         name = str; 
     315      } else if (!name) { 
     316         // couldn't find a name for the component: 
     317         var logStr = "jala.Form encountered component of type " + clazzName.toLowerCase() + " without name or label property in config of form "; 
     318         logStr += (container.form) ? container.form.name : container.name; 
     319         app.log(logStr); 
     320         continue; 
     321      } 
     322      var component = new constr(name); 
    308323      container.addComponent(component);  // make sure that component.form is set before the loop! 
    309324      for (var key in element) { 
     
    322337               component[key] = element[key]; 
    323338               break; 
    324             case jala.Form.REQUIRED: 
     339            case jala.Form.REQUIRE: 
    325340               component.require(key, true, msg); 
    326341               break; 
     
    405420       name: this.createDomId("submit"), 
    406421       "class": "submit", 
    407        "value": this.getSubmitCaption() || "Submit"} 
     422       "value": this.getSubmitValue() || "Submit"} 
    408423   ); 
    409424   jala.Form.html.closeTag("div"); 
     
    475490 * @param {Object} destObj (optional) object whose values will be changed. 
    476491 *       By default the dataObj passed to the constructor or to 
    477  *       setDataObj is used. 
     492 *       setDataObject is used. 
    478493 */ 
    479494jala.Form.prototype.save = function(tracker, destObj) { 
    480495   tracker = (tracker) ? tracker : this.getTracker(); 
    481    destObj = (destObj) ? destObj : this.getDataObj(); 
     496   destObj = (destObj) ? destObj : this.getDataObject(); 
    482497   var components = this.listComponents(); 
    483498   for (var i=0; i<components.length; i++) { 
     
    559574 * Constant used by require function to define that a component 
    560575 * should validate only if the user did provide input. 
    561  * Value: "required" 
    562  * @type String 
    563  */ 
    564 jala.Form.REQUIRED      = "required"; 
     576 * Value: "require" 
     577 * @type String 
     578 */ 
     579jala.Form.REQUIRE       = "require"; 
    565580 
    566581/** 
     
    632647 */ 
    633648jala.Form.Component = function Component(name) { 
     649    
     650   if (!name) { 
     651      throw "jala.Form.Component constructed without name."; 
     652   } 
     653    
    634654   /** 
    635655    * The Form this component belongs to 
     
    881901 */ 
    882902jala.Form.Component.Skin.prototype.render = function() { 
    883    var obj = (this.getHandler()) ? this.getHandler() : this.form.getDataObj(); 
     903   var obj = (this.getHandler()) ? this.getHandler() : this.form.getDataObject(); 
    884904   obj.renderSkin(this.name, this); 
    885905   return; 
     
    905925   /** 
    906926    * Sets a requirement for this component. 
    907     * If function is called without arguments, jala.Form.REQUIRED 
     927    * If function is called without arguments, jala.Form.REQUIRE 
    908928    * is set to true. 
    909929    * @param {String} key String defining the type of requirement, 
     
    916936      if (arguments.length == 0) { 
    917937         // set default value for arguments 
    918          key = jala.Form.REQUIRED; 
     938         key = jala.Form.REQUIRE; 
    919939         val = true; 
    920940      } 
     
    950970    * Returns a specific message for a config element. 
    951971    * @param {String} key The key of the message as defined by 
    952     *          the constants in jala.Form.* (e.g. "required", 
     972    *          the constants in jala.Form.* (e.g. "require", 
    953973    *          "maxlength", "minlength" ... 
    954974    * @param {String} defaultMsg the message to use when no message 
     
    11121132      if (this.validator) { 
    11131133         error = this.validator.call( 
    1114             this.form.getDataObj(), 
     1134            this.form.getDataObject(), 
    11151135            this.name, 
    11161136            tracker.values[this.name], 
     
    11561176      return null; 
    11571177   } else if (this.getter) { 
    1158       return this.getter.call(this.form.getDataObj(), this.name); 
     1178      return this.getter.call(this.form.getDataObject(), this.name); 
    11591179   } else { 
    1160       return this.form.getDataObj()[this.name]; 
     1180      return this.form.getDataObject()[this.name]; 
    11611181   } 
    11621182}; 
     
    11971217 */ 
    11981218jala.Form.Component.Input.prototype.render = function() { 
    1199    var className = (this.getRequirement(jala.Form.REQUIRED) == true) ? "required" : "optional"; 
     1219   var className = (this.getRequirement(jala.Form.REQUIRE) == true) ? "require" : "optional"; 
    12001220   if (this.getClassName()) { 
    12011221      className += " " + this.getClassName(); 
     
    13501370 
    13511371/** 
    1352  * Checks user input for maximum length, minimum length and required 
     1372 * Checks user input for maximum length, minimum length and require 
    13531373 * if the corresponding options have been set using the require method. 
    13541374 * @param {Object} reqData request data 
     
    13581378 */ 
    13591379jala.Form.Component.Input.prototype.checkLength = function(reqData) { 
    1360    var required  = this.getRequirement(jala.Form.REQUIRED); 
     1380   var require   = this.getRequirement(jala.Form.REQUIRE); 
    13611381   var minLength = this.getRequirement(jala.Form.MINLENGTH); 
    13621382   var maxLength = this.getRequirement(jala.Form.MAXLENGTH); 
    13631383    
    1364    if (required && (reqData[this.name] == null || reqData[this.name].trim() == "")) { 
    1365       return this.getMessage(jala.Form.REQUIRED, "Please enter text into this field."); 
     1384   if (require && (reqData[this.name] == null || reqData[this.name].trim() == "")) { 
     1385      return this.getMessage(jala.Form.REQUIRE, "Please enter text into this field."); 
    13661386   } else if (maxLength && reqData[this.name].length > maxLength) { 
    13671387      return this.getMessage(jala.Form.MAXLENGTH, "Input for this field is too long ({0} characters). Please enter no more than {1} characters.", 
     
    13711391      // but don't throw an error if the element is optional and empty 
    13721392      if (reqData[this.name].length < minLength && 
    1373           (required || (!required && reqData[this.name].length > 0))) { 
     1393          (require || (!require && reqData[this.name].length > 0))) { 
    13741394         return this.getMessage(jala.Form.MINLENGTH, "Input for this field is too short ({0} characters). Please enter at least {1} characters.", 
    13751395               reqData[this.name].length, minLength); 
     
    17741794         return options; 
    17751795      } else if (options instanceof Function) { 
    1776          return options.call(this.form.getDataObj(), this.name); 
     1796         return options.call(this.form.getDataObject(), this.name); 
    17771797      } 
    17781798   } 
     
    17891809jala.Form.Component.Select.prototype.checkOptions = function(reqData) { 
    17901810   // if field is required, an empty option is not allowed: 
    1791    var found = (!this.getRequirement(jala.Form.REQUIRED) && !reqData[this.name]); 
     1811   var found = (!this.getRequirement(jala.Form.REQUIRE) && !reqData[this.name]); 
    17921812   if (!found) { 
    17931813      if (this.getRequirement(jala.Form.CHECKOPTIONS) === false) { 
     
    19862006 
    19872007/** 
    1988  * Validates a file upload by making sure it's there (if REQUIRED is set), 
     2008 * Validates a file upload by making sure it's there (if REQUIRE is set), 
    19892009 * checking the file size, the content type and by trying to construct an image. 
    19902010 * @param {Object} reqData request data 
     
    19972017   if (reqData[this.name].contentLength == 0) { 
    19982018      // no upload 
    1999       if (this.getRequirement(jala.Form.REQUIRED) == true) { 
    2000          return this.getMessage(jala.Form.REQUIRED, "File upload is required."); 
     2019      if (this.getRequirement(jala.Form.REQUIRE) == true) { 
     2020         return this.getMessage(jala.Form.REQUIRE, "File upload is required."); 
    20012021      } else { 
    20022022         // no further checks necessary, exit here 
     
    20502070 
    20512071/** 
    2052  * Validates an image upload by making sure it's there (if REQUIRED is set), 
     2072 * Validates an image upload by making sure it's there (if REQUIRE is set), 
    20532073 * checking the file size, the content type and by trying to construct an image. 
    20542074 * If the file is an image, width and height limitations set by require are 
  • jala/trunk/tests/Form.js

    r230 r231  
    7070   assertEqual(list[++idx].name, "div"); 
    7171   assertAttribute(list[idx].attributes, "id", "test_alias_row"); 
    72    assertAttribute(list[idx].attributes, "class", "row required"); 
     72   assertAttribute(list[idx].attributes, "class", "row require"); 
    7373 
    7474   assertEqual(list[++idx].name, "label"); 
     
    9696   assertEqual(list[++idx].name, "div"); 
    9797   assertAttribute(list[idx].attributes, "id", "test_desc_row"); 
    98    assertAttribute(list[idx].attributes, "class", "row required"); 
     98   assertAttribute(list[idx].attributes, "class", "row require"); 
    9999 
    100100   assertEqual(list[++idx].name, "label"); 
     
    117117   assertEqual(list[++idx].name, "div"); 
    118118   assertAttribute(list[idx].attributes, "id", "test_pushdate_row"); 
    119    assertAttribute(list[idx].attributes, "class", "row required"); 
     119   assertAttribute(list[idx].attributes, "class", "row require"); 
    120120 
    121121   assertEqual(list[++idx].name, "label"); 
     
    242242   assertAttribute(list[idx].attributes, "class", "submit"); 
    243243   assertAttribute(list[idx].attributes, "name", "test_submit"); 
    244    assertAttribute(list[idx].attributes, "value", "Submit"); 
     244   assertAttribute(list[idx].attributes, "value", "Save"); 
    245245   assertAttribute(list[idx].attributes, "type", "submit"); 
    246246    
     
    308308 */ 
    309309var testFormSave = function() { 
    310    var dataObj = form.getDataObj(); 
     310   var dataObj = form.getDataObject(); 
    311311 
    312312   var reqData = getRequestData(); 
     
    400400   return   { 
    401401   name:             "test", 
    402    submit:           "Save", 
     402   submitValue:      "Save", 
    403403   components:[ 
    404404      { 
     
    408408         minlength:  4, 
    409409         maxlength:  10, 
    410          required:   true, 
     410         require:    true, 
    411411         messages:   { 
    412             required: "Alias is required.", 
     412            require "Alias is required.", 
    413413            maxlength: "Alias is too long.", 
    414414            minlength: "Alias is too short." 
     
    420420         rows:       3, 
    421421         cols:       30, 
    422          required:   true, 
     422         require:    true, 
    423423         getter:     function(name) { 
    424424            return this.getProperty(name); 
     
    432432         type:       "date", 
    433433         dateFormat: "d.M.yyyy H:m", 
    434          required:   true 
     434         require:    true 
    435435      }, 
    436436      {