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