-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-form.html
58 lines (57 loc) · 2.03 KB
/
data-form.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
<dom-module id="data-form">
<script>
Polymer({
is: 'data-form',
extends: 'form',
hostAttributes: {
'novalidate': true
},
properties: {
reference: { //The object we can bind to in templates.
type: Object,
readOnly: true,
notify: true,
value: function() { return { valid: true }; }
}
},
listeners: {
'data-elmement-added': '_onChildAdded',
'validation': '_onValidation'
},
attached: function() {
this.addEventListener('submit', this._onSubmit.bind(this));
},
validate: function() {
var result = true;
for(var field in this.reference) {
var elm = this.reference[field].$elm;
if(elm && !elm.validate()) {
result = false;
break;
}
}
return result;
},
_onChildAdded: function(event) {
var detail = event.detail;
this.set(['reference', detail.field], {
valid: detail.valid,
$elm: detail.$elm
});
},
_onSubmit: function(event) {
event.preventDefault();
if(this.validate()) {
this.removeEventListener('submit', this._onSubmit);
this.submit();
console.log('All good, chief!');
}
},
_onValidation: function(event) {
var detail = event.detail;
this.set(['reference', detail.field, 'valid'], detail.valid);
this.set(['reference', detail.field, 'error'], detail.error);
},
});
</script>
</dom-module>