| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 ``<iron-form>` is an HTML `<form>` element that can validate and submit any cust
om | 3 ``<iron-form>` is an HTML `<form>` element that can validate and submit any cust
om |
| 4 elements that implement `Polymer.IronFormElementBehavior`, as well as any | 4 elements that implement `Polymer.IronFormElementBehavior`, as well as any |
| 5 native HTML elements. | 5 native HTML elements. |
| 6 | 6 |
| 7 It supports both `get` and `post` methods, and uses an `iron-ajax` element to | 7 It supports both `get` and `post` methods, and uses an `iron-ajax` element to |
| 8 submit the form data to the action URL. | 8 submit the form data to the action URL. |
| 9 | 9 |
| 10 Example: | 10 Example: |
| 11 | 11 |
| 12 <form is="iron-form" id="form" method="post" action="/form/handler"> | 12 <form is="iron-form" id="form" method="post" action="/form/handler"> |
| 13 <paper-input name="name" label="name"></paper-input> | 13 <paper-input name="name" label="name"></paper-input> |
| 14 <input name="address"> | 14 <input name="address"> |
| 15 ... | 15 ... |
| 16 </form> | 16 </form> |
| 17 | 17 |
| 18 By default, a native `<button>` element will submit this form. However, if you | 18 By default, a native `<button>` element will submit this form. However, if you |
| 19 want to submit it from a custom element's click handler, you need to explicitly | 19 want to submit it from a custom element's click handler, you need to explicitly |
| 20 call the form's `submit` method. | 20 call the form's `submit` method. |
| 21 | 21 |
| 22 Example: | 22 Example: |
| 23 | 23 |
| 24 <paper-button raised onclick="submitForm()">Submit</paper-button> | 24 <paper-button raised onclick="submitForm()">Submit</paper-button> |
| 25 | 25 |
| 26 function submitForm() { | 26 function submitForm() { |
| 27 document.getElementById('form').submit(); | 27 document.getElementById('form').submit(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 @demo demo/index.html | 30 @demo demo/index.html |
| 31 */ | 31 */ |
| 32 | 32 |
| 33 Polymer({ | 33 Polymer({ |
| 34 | 34 |
| 35 is: 'iron-form', | 35 is: 'iron-form', |
| 36 | 36 |
| 37 extends: 'form', | 37 extends: 'form', |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Fired if the form cannot be submitted because it's invalid. |
| 41 * |
| 42 * @event iron-form-invalid |
| 43 */ |
| 44 |
| 45 /** |
| 40 * Fired after the form is submitted. | 46 * Fired after the form is submitted. |
| 41 * | 47 * |
| 42 * @event iron-form-submit | 48 * @event iron-form-submit |
| 43 */ | 49 */ |
| 44 | 50 |
| 45 /** | 51 /** |
| 46 * Fired after the form is submitted and a response is received. | 52 * Fired after the form is submitted and a response is received. |
| 47 * | 53 * |
| 48 * @event iron-form-response | 54 * @event iron-form-response |
| 49 */ | 55 */ |
| 50 | 56 |
| 51 /** | 57 /** |
| 52 * Fired after the form is submitted and an error is received. | 58 * Fired after the form is submitted and an error is received. |
| 53 * | 59 * |
| 54 * @event iron-form-error | 60 * @event iron-form-error |
| 55 */ | 61 */ |
| 56 | 62 |
| 57 listeners: { | 63 listeners: { |
| 58 'iron-form-element-register': '_registerElement', | 64 'iron-form-element-register': '_registerElement', |
| 59 'submit': 'submit' | 65 'submit': '_onSubmit' |
| 60 }, | 66 }, |
| 61 | 67 |
| 62 ready: function() { | 68 ready: function() { |
| 63 // Object that handles the ajax form submission request. | 69 // Object that handles the ajax form submission request. |
| 64 this._requestBot = document.createElement('iron-ajax'); | 70 this._requestBot = document.createElement('iron-ajax'); |
| 65 this._requestBot.addEventListener('response', this._handleFormResponse.bin
d(this)); | 71 this._requestBot.addEventListener('response', this._handleFormResponse.bin
d(this)); |
| 66 this._requestBot.addEventListener('error', this._handleFormError.bind(this
)); | 72 this._requestBot.addEventListener('error', this._handleFormError.bind(this
)); |
| 67 | 73 |
| 68 // Holds all the custom elements registered with this form. | 74 // Holds all the custom elements registered with this form. |
| 69 this._customElements = []; | 75 this._customElements = []; |
| 70 }, | 76 }, |
| 71 | 77 |
| 72 /** | 78 /** |
| 73 * Called to submit the form. | 79 * Called to submit the form. |
| 74 */ | 80 */ |
| 75 submit: function(event) { | 81 submit: function() { |
| 76 if (!this._validate()) { | 82 if (!this.noValidate && !this._validate()) { |
| 77 return false; | 83 |
| 84 // In order to trigger the native browser invalid-form UI, we need |
| 85 // to do perform a fake form submit. |
| 86 this._doFakeSubmitForValidation(); |
| 87 this.fire('iron-form-invalid'); |
| 88 return; |
| 78 } | 89 } |
| 79 | 90 |
| 80 var json = this.serialize(); | 91 var json = this.serialize(); |
| 81 | 92 |
| 82 this._requestBot.url = this.action; | 93 this._requestBot.url = this.action; |
| 83 this._requestBot.method = this.method; | 94 this._requestBot.method = this.method; |
| 84 this._requestBot.params = json; | 95 this._requestBot.params = json; |
| 85 | 96 |
| 86 if (this.method == 'POST') { | 97 if (this.method == 'POST') { |
| 87 this._requestBot.body = JSON.stringify(json); | 98 this._requestBot.body = JSON.stringify(json); |
| 88 } | 99 } |
| 89 | 100 |
| 90 this._requestBot.generateRequest(); | 101 this._requestBot.generateRequest(); |
| 91 this.fire('iron-form-submit', json); | 102 this.fire('iron-form-submit', json); |
| 103 }, |
| 104 |
| 105 _onSubmit: function(event) { |
| 106 this.submit(); |
| 92 | 107 |
| 93 // Don't perform a page refresh. | 108 // Don't perform a page refresh. |
| 94 if (event) { | 109 if (event) { |
| 95 event.preventDefault(); | 110 event.preventDefault(); |
| 96 } | 111 } |
| 97 | 112 |
| 98 return false; | 113 return false; |
| 99 }, | 114 }, |
| 100 | 115 |
| 101 /** | 116 /** |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 _handleFormError: function (event) { | 166 _handleFormError: function (event) { |
| 152 this.fire('iron-form-error', event.detail); | 167 this.fire('iron-form-error', event.detail); |
| 153 }, | 168 }, |
| 154 | 169 |
| 155 _registerElement: function(e) { | 170 _registerElement: function(e) { |
| 156 this._customElements.push(e.target); | 171 this._customElements.push(e.target); |
| 157 }, | 172 }, |
| 158 | 173 |
| 159 _validate: function() { | 174 _validate: function() { |
| 160 var valid = true; | 175 var valid = true; |
| 176 |
| 177 // Validate all the custom elements. |
| 178 var validatable; |
| 161 for (var el, i = 0; el = this._customElements[i], i < this._customElements
.length; i++) { | 179 for (var el, i = 0; el = this._customElements[i], i < this._customElements
.length; i++) { |
| 162 valid = el.validate() && valid; | 180 if (el.required) { |
| 181 validatable = /** @type {{validate: (function() : boolean)}} */ (el); |
| 182 valid = validatable.validate() && valid; |
| 183 } |
| 163 } | 184 } |
| 185 |
| 186 // Validate the form's native elements. |
| 187 for (var el, i = 0; el = this.elements[i], i < this.elements.length; i++)
{ |
| 188 // Custom elements that extend a native element will also appear in |
| 189 // this list, but they've already been validated. |
| 190 if (!el.hasAttribute('is') && el.willValidate && el.checkValidity) { |
| 191 valid = el.checkValidity() && valid; |
| 192 } |
| 193 } |
| 194 |
| 164 return valid; | 195 return valid; |
| 165 }, | 196 }, |
| 166 | 197 |
| 167 _useValue: function(el) { | 198 _useValue: function(el) { |
| 168 // Checkboxes and radio buttons should only use their value if they're che
cked. | 199 // Checkboxes and radio buttons should only use their value if they're che
cked. |
| 169 if (el.type !== 'checkbox' && el.type !== 'radio') { | 200 if (el.type !== 'checkbox' && el.type !== 'radio') { |
| 170 return true; | 201 return true; |
| 171 } else { | 202 } else { |
| 172 return el.checked; | 203 return el.checked; |
| 173 } | 204 } |
| 205 }, |
| 206 |
| 207 _doFakeSubmitForValidation: function() { |
| 208 var fakeSubmit = document.createElement('input'); |
| 209 fakeSubmit.setAttribute('type', 'submit'); |
| 210 fakeSubmit.style.display = 'none'; |
| 211 this.appendChild(fakeSubmit); |
| 212 |
| 213 fakeSubmit.click(); |
| 214 |
| 215 this.removeChild(fakeSubmit); |
| 174 } | 216 } |
| 175 | 217 |
| 176 }); | 218 }); |
| 177 | 219 |
| OLD | NEW |