| OLD | NEW |
| 1 /* | 1 /* |
| 2 `<iron-input>` adds two-way binding and custom validators using `Polymer.IronVal
idatorBehavior` | 2 `<iron-input>` adds two-way binding and custom validators using `Polymer.IronVal
idatorBehavior` |
| 3 to `<input>`. | 3 to `<input>`. |
| 4 | 4 |
| 5 ### Two-way binding | 5 ### Two-way binding |
| 6 | 6 |
| 7 By default you can only get notified of changes to an `input`'s `value` due to u
ser input: | 7 By default you can only get notified of changes to an `input`'s `value` due to u
ser input: |
| 8 | 8 |
| 9 <input value="{{myValue::input}}"> | 9 <input value="{{myValue::input}}"> |
| 10 | 10 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * Use this property instead of `value` for two-way data binding. | 48 * Use this property instead of `value` for two-way data binding. |
| 49 */ | 49 */ |
| 50 bindValue: { | 50 bindValue: { |
| 51 observer: '_bindValueChanged', | 51 observer: '_bindValueChanged', |
| 52 type: String | 52 type: String |
| 53 }, | 53 }, |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Set to true to prevent the user from entering invalid input. The new in
put characters are | 56 * Set to true to prevent the user from entering invalid input. If `allowe
dPattern` is set, |
| 57 * matched with `allowedPattern` if it is set, otherwise it will use the `
type` attribute (only | 57 * any character typed by the user will be matched against that pattern, a
nd rejected if it's not a match. |
| 58 * supported for `type=number`). | 58 * Pasted input will have each character checked individually; if any char
acter |
| 59 * doesn't match `allowedPattern`, the entire pasted string will be reject
ed. |
| 60 * If `allowedPattern` is not set, it will use the `type` attribute (only
supported for `type=number`). |
| 59 */ | 61 */ |
| 60 preventInvalidInput: { | 62 preventInvalidInput: { |
| 61 type: Boolean | 63 type: Boolean |
| 62 }, | 64 }, |
| 63 | 65 |
| 64 /** | 66 /** |
| 65 * Regular expression expressing a set of characters to enforce the validi
ty of input characters. | 67 * Regular expression that list the characters allowed as input. |
| 66 * The recommended value should follow this format: `[a-ZA-Z0-9.+-!;:]` th
at list the characters | 68 * This pattern represents the allowed characters for the field; as the us
er inputs text, |
| 67 * allowed as input. | 69 * each individual character will be checked against the pattern (rather t
han checking |
| 70 * the entire value as a whole). The recommended format should be a list o
f allowed characters; |
| 71 * for example, `[a-zA-Z0-9.+-!;:]` |
| 68 */ | 72 */ |
| 69 allowedPattern: { | 73 allowedPattern: { |
| 70 type: String, | 74 type: String, |
| 71 observer: "_allowedPatternChanged" | 75 observer: "_allowedPatternChanged" |
| 72 }, | 76 }, |
| 73 | 77 |
| 74 _previousValidInput: { | 78 _previousValidInput: { |
| 75 type: String, | 79 type: String, |
| 76 value: '' | 80 value: '' |
| 77 }, | 81 }, |
| 78 | 82 |
| 79 _patternAlreadyChecked: { | 83 _patternAlreadyChecked: { |
| 80 type: Boolean, | 84 type: Boolean, |
| 81 value: false | 85 value: false |
| 82 } | 86 } |
| 83 | 87 |
| 84 }, | 88 }, |
| 85 | 89 |
| 86 listeners: { | 90 listeners: { |
| 87 'input': '_onInput', | 91 'input': '_onInput', |
| 88 'keypress': '_onKeypress' | 92 'keypress': '_onKeypress' |
| 89 }, | 93 }, |
| 90 | 94 |
| 95 /** @suppress {checkTypes} */ |
| 96 registered: function() { |
| 97 // Feature detect whether we need to patch dispatchEvent (i.e. on FF and I
E). |
| 98 if (!this._canDispatchEventOnDisabled()) { |
| 99 this._origDispatchEvent = this.dispatchEvent; |
| 100 this.dispatchEvent = this._dispatchEventFirefoxIE; |
| 101 } |
| 102 }, |
| 103 |
| 104 created: function() { |
| 105 Polymer.IronA11yAnnouncer.requestAvailability(); |
| 106 }, |
| 107 |
| 108 _canDispatchEventOnDisabled: function() { |
| 109 var input = document.createElement('input'); |
| 110 var canDispatch = false; |
| 111 input.disabled = true; |
| 112 |
| 113 input.addEventListener('feature-check-dispatch-event', function() { |
| 114 canDispatch = true; |
| 115 }); |
| 116 |
| 117 try { |
| 118 input.dispatchEvent(new Event('feature-check-dispatch-event')); |
| 119 } catch(e) {} |
| 120 |
| 121 return canDispatch; |
| 122 }, |
| 123 |
| 124 _dispatchEventFirefoxIE: function() { |
| 125 // Due to Firefox bug, events fired on disabled form controls can throw |
| 126 // errors; furthermore, neither IE nor Firefox will actually dispatch |
| 127 // events from disabled form controls; as such, we toggle disable around |
| 128 // the dispatch to allow notifying properties to notify |
| 129 // See issue #47 for details |
| 130 var disabled = this.disabled; |
| 131 this.disabled = false; |
| 132 this._origDispatchEvent.apply(this, arguments); |
| 133 this.disabled = disabled; |
| 134 }, |
| 135 |
| 91 get _patternRegExp() { | 136 get _patternRegExp() { |
| 92 var pattern; | 137 var pattern; |
| 93 if (this.allowedPattern) { | 138 if (this.allowedPattern) { |
| 94 pattern = new RegExp(this.allowedPattern); | 139 pattern = new RegExp(this.allowedPattern); |
| 95 } else { | 140 } else { |
| 96 switch (this.type) { | 141 switch (this.type) { |
| 97 case 'number': | 142 case 'number': |
| 98 pattern = /[0-9.,e-]/; | 143 pattern = /[0-9.,e-]/; |
| 99 break; | 144 break; |
| 100 } | 145 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 121 // Force to prevent invalid input when an `allowed-pattern` is set | 166 // Force to prevent invalid input when an `allowed-pattern` is set |
| 122 this.preventInvalidInput = this.allowedPattern ? true : false; | 167 this.preventInvalidInput = this.allowedPattern ? true : false; |
| 123 }, | 168 }, |
| 124 | 169 |
| 125 _onInput: function() { | 170 _onInput: function() { |
| 126 // Need to validate each of the characters pasted if they haven't | 171 // Need to validate each of the characters pasted if they haven't |
| 127 // been validated inside `_onKeypress` already. | 172 // been validated inside `_onKeypress` already. |
| 128 if (this.preventInvalidInput && !this._patternAlreadyChecked) { | 173 if (this.preventInvalidInput && !this._patternAlreadyChecked) { |
| 129 var valid = this._checkPatternValidity(); | 174 var valid = this._checkPatternValidity(); |
| 130 if (!valid) { | 175 if (!valid) { |
| 176 this._announceInvalidCharacter('Invalid string of characters not enter
ed.'); |
| 131 this.value = this._previousValidInput; | 177 this.value = this._previousValidInput; |
| 132 } | 178 } |
| 133 } | 179 } |
| 134 | 180 |
| 135 this.bindValue = this.value; | 181 this.bindValue = this.value; |
| 136 this._previousValidInput = this.value; | 182 this._previousValidInput = this.value; |
| 137 this._patternAlreadyChecked = false; | 183 this._patternAlreadyChecked = false; |
| 138 }, | 184 }, |
| 139 | 185 |
| 140 _isPrintable: function(event) { | 186 _isPrintable: function(event) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 // Handle special keys and backspace | 227 // Handle special keys and backspace |
| 182 if (event.metaKey || event.ctrlKey || event.altKey) | 228 if (event.metaKey || event.ctrlKey || event.altKey) |
| 183 return; | 229 return; |
| 184 | 230 |
| 185 // Check the pattern either here or in `_onInput`, but not in both. | 231 // Check the pattern either here or in `_onInput`, but not in both. |
| 186 this._patternAlreadyChecked = true; | 232 this._patternAlreadyChecked = true; |
| 187 | 233 |
| 188 var thisChar = String.fromCharCode(event.charCode); | 234 var thisChar = String.fromCharCode(event.charCode); |
| 189 if (this._isPrintable(event) && !regexp.test(thisChar)) { | 235 if (this._isPrintable(event) && !regexp.test(thisChar)) { |
| 190 event.preventDefault(); | 236 event.preventDefault(); |
| 237 this._announceInvalidCharacter('Invalid character ' + thisChar + ' not e
ntered.'); |
| 191 } | 238 } |
| 192 }, | 239 }, |
| 193 | 240 |
| 194 _checkPatternValidity: function() { | 241 _checkPatternValidity: function() { |
| 195 var regexp = this._patternRegExp; | 242 var regexp = this._patternRegExp; |
| 196 if (!regexp) { | 243 if (!regexp) { |
| 197 return true; | 244 return true; |
| 198 } | 245 } |
| 199 for (var i = 0; i < this.value.length; i++) { | 246 for (var i = 0; i < this.value.length; i++) { |
| 200 if (!regexp.test(this.value[i])) { | 247 if (!regexp.test(this.value[i])) { |
| 201 return false; | 248 return false; |
| 202 } | 249 } |
| 203 } | 250 } |
| 204 return true; | 251 return true; |
| 205 }, | 252 }, |
| 206 | 253 |
| 207 /** | 254 /** |
| 208 * Returns true if `value` is valid. The validator provided in `validator` w
ill be used first, | 255 * Returns true if `value` is valid. The validator provided in `validator` w
ill be used first, |
| 209 * then any constraints. | 256 * then any constraints. |
| 210 * @return {boolean} True if the value is valid. | 257 * @return {boolean} True if the value is valid. |
| 211 */ | 258 */ |
| 212 validate: function() { | 259 validate: function() { |
| 213 // Empty, non-required input is valid. | 260 // First, check what the browser thinks. Some inputs (like type=number) |
| 214 if (!this.required && this.value == '') { | 261 // behave weirdly and will set the value to "" if something invalid is |
| 215 this.invalid = false; | 262 // entered, but will set the validity correctly. |
| 216 return true; | 263 var valid = this.checkValidity(); |
| 264 |
| 265 // Only do extra checking if the browser thought this was valid. |
| 266 if (valid) { |
| 267 // Empty, required input is invalid |
| 268 if (this.required && this.value === '') { |
| 269 valid = false; |
| 270 } else if (this.hasValidator()) { |
| 271 valid = Polymer.IronValidatableBehavior.validate.call(this, this.value
); |
| 272 } |
| 217 } | 273 } |
| 218 | 274 |
| 219 var valid; | 275 this.invalid = !valid; |
| 220 if (this.hasValidator()) { | |
| 221 valid = Polymer.IronValidatableBehavior.validate.call(this, this.value); | |
| 222 } else { | |
| 223 valid = this.checkValidity(); | |
| 224 this.invalid = !valid; | |
| 225 } | |
| 226 this.fire('iron-input-validate'); | 276 this.fire('iron-input-validate'); |
| 227 return valid; | 277 return valid; |
| 278 }, |
| 279 |
| 280 _announceInvalidCharacter: function(message) { |
| 281 this.fire('iron-announce', { text: message }); |
| 228 } | 282 } |
| 229 | |
| 230 }); | 283 }); |
| 231 | 284 |
| 232 /* | 285 /* |
| 233 The `iron-input-validate` event is fired whenever `validate()` is called. | 286 The `iron-input-validate` event is fired whenever `validate()` is called. |
| 234 @event iron-input-validate | 287 @event iron-input-validate |
| 235 */ | 288 */ |
| OLD | NEW |