Form.js 5.0 KB

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