OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 /** |
| 4 * Use `Polymer.IronValidatableBehavior` to implement an element that validate
s user input. |
| 5 * |
| 6 * ### Accessiblity |
| 7 * |
| 8 * Changing the `invalid` property, either manually or by calling `validate()`
will update the |
| 9 * `aria-invalid` attribute. |
| 10 * |
| 11 * @demo demo/index.html |
| 12 * @polymerBehavior |
| 13 */ |
| 14 Polymer.IronValidatableBehavior = { |
| 15 |
| 16 properties: { |
| 17 |
| 18 /** |
| 19 * Namespace for this validator. |
| 20 */ |
| 21 validatorType: { |
| 22 type: String, |
| 23 value: 'validator' |
| 24 }, |
| 25 |
| 26 /** |
| 27 * Name of the validator to use. |
| 28 */ |
| 29 validator: { |
| 30 type: String |
| 31 }, |
| 32 |
| 33 /** |
| 34 * True if the last call to `validate` is invalid. |
| 35 */ |
| 36 invalid: { |
| 37 reflectToAttribute: true, |
| 38 type: Boolean, |
| 39 value: false |
| 40 }, |
| 41 |
| 42 _validatorMeta: { |
| 43 type: Object |
| 44 } |
| 45 |
| 46 }, |
| 47 |
| 48 observers: [ |
| 49 '_invalidChanged(invalid)' |
| 50 ], |
| 51 |
| 52 get _validator() { |
| 53 return this._validatorMeta && this._validatorMeta.byKey(this.validator); |
| 54 }, |
| 55 |
| 56 ready: function() { |
| 57 this._validatorMeta = new Polymer.IronMeta({type: this.validatorType}); |
| 58 }, |
| 59 |
| 60 _invalidChanged: function() { |
| 61 if (this.invalid) { |
| 62 this.setAttribute('aria-invalid', 'true'); |
| 63 } else { |
| 64 this.removeAttribute('aria-invalid'); |
| 65 } |
| 66 }, |
| 67 |
| 68 /** |
| 69 * @return {Boolean} True if the validator `validator` exists. |
| 70 */ |
| 71 hasValidator: function() { |
| 72 return this._validator != null; |
| 73 }, |
| 74 |
| 75 /** |
| 76 * @param {Object} values Passed to the validator's `validate()` function. |
| 77 * @return {Boolean} True if `values` is valid. |
| 78 */ |
| 79 validate: function(values) { |
| 80 var valid = this._validator && this._validator.validate(values); |
| 81 this.invalid = !valid; |
| 82 return valid; |
| 83 } |
| 84 |
| 85 }; |
| 86 |
OLD | NEW |