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