Form.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit.form.Form"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.form.Form"] = true;
  8. dojo.provide("dijit.form.Form");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.require("dijit.form._FormMixin");
  12. dojo.require("dijit.layout._ContentPaneResizeMixin");
  13. dojo.declare(
  14. "dijit.form.Form",
  15. [dijit._Widget, dijit._Templated, dijit.form._FormMixin, dijit.layout._ContentPaneResizeMixin],
  16. {
  17. // summary:
  18. // Widget corresponding to HTML form tag, for validation and serialization
  19. //
  20. // example:
  21. // | <form dojoType="dijit.form.Form" id="myForm">
  22. // | Name: <input type="text" name="name" />
  23. // | </form>
  24. // | myObj = {name: "John Doe"};
  25. // | dijit.byId('myForm').set('value', myObj);
  26. // |
  27. // | myObj=dijit.byId('myForm').get('value');
  28. // HTML <FORM> attributes
  29. // name: String?
  30. // Name of form for scripting.
  31. name: "",
  32. // action: String?
  33. // Server-side form handler.
  34. action: "",
  35. // method: String?
  36. // HTTP method used to submit the form, either "GET" or "POST".
  37. method: "",
  38. // encType: String?
  39. // Encoding type for the form, ex: application/x-www-form-urlencoded.
  40. encType: "",
  41. // accept-charset: String?
  42. // List of supported charsets.
  43. "accept-charset": "",
  44. // accept: String?
  45. // List of MIME types for file upload.
  46. accept: "",
  47. // target: String?
  48. // Target frame for the document to be opened in.
  49. target: "",
  50. templateString: "<form dojoAttachPoint='containerNode' dojoAttachEvent='onreset:_onReset,onsubmit:_onSubmit' ${!nameAttrSetting}></form>",
  51. attributeMap: dojo.delegate(dijit._Widget.prototype.attributeMap, {
  52. action: "",
  53. method: "",
  54. encType: "",
  55. "accept-charset": "",
  56. accept: "",
  57. target: ""
  58. }),
  59. postMixInProperties: function(){
  60. // Setup name=foo string to be referenced from the template (but only if a name has been specified)
  61. // Unfortunately we can't use attributeMap to set the name due to IE limitations, see #8660
  62. this.nameAttrSetting = this.name ? ("name='" + this.name + "'") : "";
  63. this.inherited(arguments);
  64. },
  65. execute: function(/*Object*/ formContents){
  66. // summary:
  67. // Deprecated: use submit()
  68. // tags:
  69. // deprecated
  70. },
  71. onExecute: function(){
  72. // summary:
  73. // Deprecated: use onSubmit()
  74. // tags:
  75. // deprecated
  76. },
  77. _setEncTypeAttr: function(/*String*/ value){
  78. this.encType = value;
  79. dojo.attr(this.domNode, "encType", value);
  80. if(dojo.isIE){ this.domNode.encoding = value; }
  81. },
  82. postCreate: function(){
  83. // IE tries to hide encType
  84. // TODO: remove in 2.0, no longer necessary with data-dojo-params
  85. if(dojo.isIE && this.srcNodeRef && this.srcNodeRef.attributes){
  86. var item = this.srcNodeRef.attributes.getNamedItem('encType');
  87. if(item && !item.specified && (typeof item.value == "string")){
  88. this.set('encType', item.value);
  89. }
  90. }
  91. this.inherited(arguments);
  92. },
  93. reset: function(/*Event?*/ e){
  94. // summary:
  95. // restores all widget values back to their init values,
  96. // calls onReset() which can cancel the reset by returning false
  97. // create fake event so we can know if preventDefault() is called
  98. var faux = {
  99. returnValue: true, // the IE way
  100. preventDefault: function(){ // not IE
  101. this.returnValue = false;
  102. },
  103. stopPropagation: function(){},
  104. currentTarget: e ? e.target : this.domNode,
  105. target: e ? e.target : this.domNode
  106. };
  107. // if return value is not exactly false, and haven't called preventDefault(), then reset
  108. if(!(this.onReset(faux) === false) && faux.returnValue){
  109. this.inherited(arguments, []);
  110. }
  111. },
  112. onReset: function(/*Event?*/ e){
  113. // summary:
  114. // Callback when user resets the form. This method is intended
  115. // to be over-ridden. When the `reset` method is called
  116. // programmatically, the return value from `onReset` is used
  117. // to compute whether or not resetting should proceed
  118. // tags:
  119. // callback
  120. return true; // Boolean
  121. },
  122. _onReset: function(e){
  123. this.reset(e);
  124. dojo.stopEvent(e);
  125. return false;
  126. },
  127. _onSubmit: function(e){
  128. var fp = dijit.form.Form.prototype;
  129. // TODO: remove this if statement beginning with 2.0
  130. if(this.execute != fp.execute || this.onExecute != fp.onExecute){
  131. dojo.deprecated("dijit.form.Form:execute()/onExecute() are deprecated. Use onSubmit() instead.", "", "2.0");
  132. this.onExecute();
  133. this.execute(this.getValues());
  134. }
  135. if(this.onSubmit(e) === false){ // only exactly false stops submit
  136. dojo.stopEvent(e);
  137. }
  138. },
  139. onSubmit: function(/*Event?*/ e){
  140. // summary:
  141. // Callback when user submits the form.
  142. // description:
  143. // This method is intended to be over-ridden, but by default it checks and
  144. // returns the validity of form elements. When the `submit`
  145. // method is called programmatically, the return value from
  146. // `onSubmit` is used to compute whether or not submission
  147. // should proceed
  148. // tags:
  149. // extension
  150. return this.isValid(); // Boolean
  151. },
  152. submit: function(){
  153. // summary:
  154. // programmatically submit form if and only if the `onSubmit` returns true
  155. if(!(this.onSubmit() === false)){
  156. this.containerNode.submit();
  157. }
  158. }
  159. }
  160. );
  161. }