forked from PolymerElements/iron-validator-behavior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iron-native-validator.html
114 lines (90 loc) · 4.22 KB
/
iron-native-validator.html
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
<!--
@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
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="iron-validator-behavior.html">
<!--
`<iron-native-validator>` is a an instantiation of a validator implementing the native
<a href="https://developer.mozilla.org/en-US/docs/Web/API/ValidityState">ValidityState API</a>.
It maps native validators 'required', 'max', 'min', 'step', 'maxlength', 'minlength', 'pattern',
'emailtype'(type=email) and 'urltype' (type=url) to the validity state
<iron-native-validator validator-name="required" message="Please fill in a value" id="instance"></iron-native-validator>
<iron-native-validator validator-name="emailtype" message="Please enter a valid e-mail address"></iron-native-validator>
These native validators need to be instantiated once, so they are ready for use on a global level.
The configured messages will be exposed by the validatable element, and will by default be shown via the paper-input.
# Shimming
When the native ValidityState API doesn't work (yet) in a certain browser, it is possible to provide your own validity
method.
@group Iron Elements
@element iron-native-validator
@hero hero.svg
@demo demo/index.html
-->
<dom-module id="iron-native-validator">
<script>
(function () {
var validityFns = {};
Polymer({
is: 'iron-native-validator',
behaviors: [
Polymer.IronValidatorBehavior
],
properties: {
/**
* Namespace for this validator, override of default 'validator'
*/
validatorType: {
type: String,
value: 'native-validator'
},
/**
* Maps native validity API to native validator names
*/
validityApiMapping: {
type: Object,
readOnly: true,
value: function () {
return {
required: 'valueMissing',
pattern: 'patternMismatch',
max: 'rangeOverflow',
min: 'rangeUnderflow',
step: 'stepMismatch',
maxlength: 'tooLong',
minlength: 'tooShort',
emailtype: 'typeMismatch',
urltype: 'typeMismatch'
};
}
}
},
ready: function () {
// Use native validity API for every validator
var mapping = this.validityApiMapping;
Object.keys(mapping).forEach(function (validator) {
if(!validityFns[validator]) {
validityFns[validator] = function (value, input) {
return !input.validity[mapping[validator]];
};
}
}, this);
},
validate: function (value, input) {
return validityFns[this.validatorName](value, input);
},
/* For (globally) overriding the validation method of a native validator.
* Should only be used when native validity API doesn't work (yet) in a certain browser
*/
overrideValidationMethod: function (validator, fn) {
validityFns[validator] = fn.bind(this);
}
});
}());
</script>
</dom-module>