-
Notifications
You must be signed in to change notification settings - Fork 18
/
iron-validatable-behavior.js
132 lines (119 loc) · 4.23 KB
/
iron-validatable-behavior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import '@polymer/polymer/polymer-legacy.js';
import {IronMeta} from '@polymer/iron-meta/iron-meta.js';
/**
* Singleton IronMeta instance.
*/
export let IronValidatableBehaviorMeta = null;
/**
* `Use IronValidatableBehavior` to implement an element that validates
* user input. Use the related `IronValidatorBehavior` to add custom
* validation logic to an iron-input.
*
* By default, an `<iron-form>` element validates its fields when the user
* presses the submit button. To validate a form imperatively, call the form's
* `validate()` method, which in turn will call `validate()` on all its
* children. By using `IronValidatableBehavior`, your custom element
* will get a public `validate()`, which will return the validity of the
* element, and a corresponding `invalid` attribute, which can be used for
* styling.
*
* To implement the custom validation logic of your element, you must override
* the protected `_getValidity()` method of this behaviour, rather than
* `validate()`. See
* [this](https://github.com/PolymerElements/iron-form/blob/master/demo/simple-element.html)
* for an example.
*
* ### Accessibility
*
* Changing the `invalid` property, either manually or by calling `validate()`
* will update the `aria-invalid` attribute.
*
* @demo demo/index.html
* @polymerBehavior
*/
export const IronValidatableBehavior = {
properties: {
/**
* Name of the validator to use.
*/
validator: {type: String},
/**
* True if the last call to `validate` is invalid.
*/
invalid: {
notify: true,
reflectToAttribute: true,
type: Boolean,
value: false,
observer: '_invalidChanged'
},
},
/** @override */
registered: function() {
IronValidatableBehaviorMeta = new IronMeta({type: 'validator'});
},
_invalidChanged: function() {
if (this.invalid) {
this.setAttribute('aria-invalid', 'true');
} else {
this.removeAttribute('aria-invalid');
}
},
/* Recompute this every time it's needed, because we don't know if the
* underlying IronValidatableBehaviorMeta has changed. */
get _validator() {
return IronValidatableBehaviorMeta &&
IronValidatableBehaviorMeta.byKey(this.validator);
},
/**
* @return {boolean} True if the validator `validator` exists.
*/
hasValidator: function() {
return this._validator != null;
},
/**
* Returns true if the `value` is valid, and updates `invalid`. If you want
* your element to have custom validation logic, do not override this method;
* override `_getValidity(value)` instead.
* @param {Object} value Deprecated: The value to be validated. By default,
* it is passed to the validator's `validate()` function, if a validator is
set.
* If this argument is not specified, then the element's `value` property
* is used, if it exists.
* @return {boolean} True if `value` is valid.
*/
validate: function(value) {
// If this is an element that also has a value property, and there was
// no explicit value argument passed, use the element's property instead.
if (value === undefined && this.value !== undefined)
this.invalid = !this._getValidity(this.value);
else
this.invalid = !this._getValidity(value);
return !this.invalid;
},
/**
* Returns true if `value` is valid. By default, it is passed
* to the validator's `validate()` function, if a validator is set. You
* should override this method if you want to implement custom validity
* logic for your element.
*
* @param {Object} value The value to be validated.
* @return {boolean} True if `value` is valid.
*/
_getValidity: function(value) {
if (this.hasValidator()) {
return this._validator.validate(value);
}
return true;
}
};