| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 @license | 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> | 9 --> |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 want to submit it from a custom element's click handler, you need to explicitly | 32 want to submit it from a custom element's click handler, you need to explicitly |
| 33 call the form's `submit` method. | 33 call the form's `submit` method. |
| 34 | 34 |
| 35 Example: | 35 Example: |
| 36 | 36 |
| 37 <paper-button raised onclick="submitForm()">Submit</paper-button> | 37 <paper-button raised onclick="submitForm()">Submit</paper-button> |
| 38 | 38 |
| 39 function submitForm() { | 39 function submitForm() { |
| 40 document.getElementById('form').submit(); | 40 document.getElementById('form').submit(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 @demo demo/index.html | 43 @demo demo/index.html |
| 44 */ | 44 */ |
| 45 | 45 |
| 46 Polymer({ | 46 Polymer({ |
| 47 | 47 |
| 48 is: 'iron-form', | 48 is: 'iron-form', |
| 49 | 49 |
| 50 extends: 'form', | 50 extends: 'form', |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Fired if the form cannot be submitted because it's invalid. |
| 54 * |
| 55 * @event iron-form-invalid |
| 56 */ |
| 57 |
| 58 /** |
| 53 * Fired after the form is submitted. | 59 * Fired after the form is submitted. |
| 54 * | 60 * |
| 55 * @event iron-form-submit | 61 * @event iron-form-submit |
| 56 */ | 62 */ |
| 57 | 63 |
| 58 /** | 64 /** |
| 59 * Fired after the form is submitted and a response is received. | 65 * Fired after the form is submitted and a response is received. |
| 60 * | 66 * |
| 61 * @event iron-form-response | 67 * @event iron-form-response |
| 62 */ | 68 */ |
| 63 | 69 |
| 64 /** | 70 /** |
| 65 * Fired after the form is submitted and an error is received. | 71 * Fired after the form is submitted and an error is received. |
| 66 * | 72 * |
| 67 * @event iron-form-error | 73 * @event iron-form-error |
| 68 */ | 74 */ |
| 69 | 75 |
| 70 listeners: { | 76 listeners: { |
| 71 'iron-form-element-register': '_registerElement', | 77 'iron-form-element-register': '_registerElement', |
| 72 'submit': 'submit' | 78 'submit': '_onSubmit' |
| 73 }, | 79 }, |
| 74 | 80 |
| 75 ready: function() { | 81 ready: function() { |
| 76 // Object that handles the ajax form submission request. | 82 // Object that handles the ajax form submission request. |
| 77 this._requestBot = document.createElement('iron-ajax'); | 83 this._requestBot = document.createElement('iron-ajax'); |
| 78 this._requestBot.addEventListener('response', this._handleFormResponse.bin
d(this)); | 84 this._requestBot.addEventListener('response', this._handleFormResponse.bin
d(this)); |
| 79 this._requestBot.addEventListener('error', this._handleFormError.bind(this
)); | 85 this._requestBot.addEventListener('error', this._handleFormError.bind(this
)); |
| 80 | 86 |
| 81 // Holds all the custom elements registered with this form. | 87 // Holds all the custom elements registered with this form. |
| 82 this._customElements = []; | 88 this._customElements = []; |
| 83 }, | 89 }, |
| 84 | 90 |
| 85 /** | 91 /** |
| 86 * Called to submit the form. | 92 * Called to submit the form. |
| 87 */ | 93 */ |
| 88 submit: function(event) { | 94 submit: function() { |
| 89 if (!this._validate()) { | 95 if (!this.noValidate && !this._validate()) { |
| 90 return false; | 96 |
| 97 // In order to trigger the native browser invalid-form UI, we need |
| 98 // to do perform a fake form submit. |
| 99 this._doFakeSubmitForValidation(); |
| 100 this.fire('iron-form-invalid'); |
| 101 return; |
| 91 } | 102 } |
| 92 | 103 |
| 93 var json = this.serialize(); | 104 var json = this.serialize(); |
| 94 | 105 |
| 95 this._requestBot.url = this.action; | 106 this._requestBot.url = this.action; |
| 96 this._requestBot.method = this.method; | 107 this._requestBot.method = this.method; |
| 97 this._requestBot.params = json; | 108 this._requestBot.params = json; |
| 98 | 109 |
| 99 if (this.method == 'POST') { | 110 if (this.method == 'POST') { |
| 100 this._requestBot.body = JSON.stringify(json); | 111 this._requestBot.body = JSON.stringify(json); |
| 101 } | 112 } |
| 102 | 113 |
| 103 this._requestBot.generateRequest(); | 114 this._requestBot.generateRequest(); |
| 104 this.fire('iron-form-submit', json); | 115 this.fire('iron-form-submit', json); |
| 116 }, |
| 117 |
| 118 _onSubmit: function(event) { |
| 119 this.submit(); |
| 105 | 120 |
| 106 // Don't perform a page refresh. | 121 // Don't perform a page refresh. |
| 107 if (event) { | 122 if (event) { |
| 108 event.preventDefault(); | 123 event.preventDefault(); |
| 109 } | 124 } |
| 110 | 125 |
| 111 return false; | 126 return false; |
| 112 }, | 127 }, |
| 113 | 128 |
| 114 /** | 129 /** |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 _handleFormError: function (event) { | 179 _handleFormError: function (event) { |
| 165 this.fire('iron-form-error', event.detail); | 180 this.fire('iron-form-error', event.detail); |
| 166 }, | 181 }, |
| 167 | 182 |
| 168 _registerElement: function(e) { | 183 _registerElement: function(e) { |
| 169 this._customElements.push(e.target); | 184 this._customElements.push(e.target); |
| 170 }, | 185 }, |
| 171 | 186 |
| 172 _validate: function() { | 187 _validate: function() { |
| 173 var valid = true; | 188 var valid = true; |
| 189 |
| 190 // Validate all the custom elements. |
| 191 var validatable; |
| 174 for (var el, i = 0; el = this._customElements[i], i < this._customElements
.length; i++) { | 192 for (var el, i = 0; el = this._customElements[i], i < this._customElements
.length; i++) { |
| 175 valid = el.validate() && valid; | 193 if (el.required) { |
| 194 validatable = /** @type {{validate: (function() : boolean)}} */ (el); |
| 195 valid = validatable.validate() && valid; |
| 196 } |
| 176 } | 197 } |
| 198 |
| 199 // Validate the form's native elements. |
| 200 for (var el, i = 0; el = this.elements[i], i < this.elements.length; i++)
{ |
| 201 // Custom elements that extend a native element will also appear in |
| 202 // this list, but they've already been validated. |
| 203 if (!el.hasAttribute('is') && el.willValidate && el.checkValidity) { |
| 204 valid = el.checkValidity() && valid; |
| 205 } |
| 206 } |
| 207 |
| 177 return valid; | 208 return valid; |
| 178 }, | 209 }, |
| 179 | 210 |
| 180 _useValue: function(el) { | 211 _useValue: function(el) { |
| 181 // Checkboxes and radio buttons should only use their value if they're che
cked. | 212 // Checkboxes and radio buttons should only use their value if they're che
cked. |
| 182 if (el.type !== 'checkbox' && el.type !== 'radio') { | 213 if (el.type !== 'checkbox' && el.type !== 'radio') { |
| 183 return true; | 214 return true; |
| 184 } else { | 215 } else { |
| 185 return el.checked; | 216 return el.checked; |
| 186 } | 217 } |
| 218 }, |
| 219 |
| 220 _doFakeSubmitForValidation: function() { |
| 221 var fakeSubmit = document.createElement('input'); |
| 222 fakeSubmit.setAttribute('type', 'submit'); |
| 223 fakeSubmit.style.display = 'none'; |
| 224 this.appendChild(fakeSubmit); |
| 225 |
| 226 fakeSubmit.click(); |
| 227 |
| 228 this.removeChild(fakeSubmit); |
| 187 } | 229 } |
| 188 | 230 |
| 189 }); | 231 }); |
| 190 | 232 |
| 191 </script> | 233 </script> |
| OLD | NEW |