Skip to content

Commit 64edf13

Browse files
reconfigure so form handles validation WIP
1 parent f29e4da commit 64edf13

4 files changed

Lines changed: 94 additions & 46 deletions

File tree

src/mm-dropdown/mm-dropdown.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<link rel="import" href="../shared/behaviors/stackable.html">
1919
<link rel="import" href="../shared/behaviors/stylable.html"/>
2020
<link rel="import" href="../shared/behaviors/resolvable.html"/>
21-
<link rel="import" href="../shared/behaviors/formable.html"/>
21+
<!-- <link rel="import" href="../shared/behaviors/formable.html"/> -->
2222
<link rel="import" href="../mm-item-recycler/mm-item-recycler.html">
2323
<link rel="import" href="../mm-icon/mm-icon.html"/>
2424
<link rel="import" href="../mm-list-item/mm-list-item.html"/>

src/mm-form/index.html

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,25 @@
4040

4141
<div class="col" span="1">
4242
<mm-header size="medium">Type a Number</mm-header>
43-
<mm-input form-id="input"
43+
<mm-input
4444
fitparent
45+
form-id="input"
4546
validation="int|empty"
4647
placeholder="Type a Number"
47-
error-target="#inputError"></mm-input>
48+
error-message="#inputError"></mm-input>
4849
<mm-inline-box id="inputError" type="error" fitparent>
4950
You need to type a number
5051
</mm-inline-box>
5152
</div>
5253

5354
<div class="col" span="1">
5455
<mm-header size="medium">Select an Item</mm-header>
55-
<mm-dropdown form-id="dropdown"
56+
<mm-dropdown
5657
fitparent
58+
form-id="dropdown"
5759
placeholder="Select an Item"
5860
validation="empty"
59-
error-target="#dropdownError">
61+
error-message="#dropdownError">
6062
<mm-list-item>List Item 1</mm-list-item>
6163
<mm-list-item>List Item 2</mm-list-item>
6264
<mm-list-item>List Item 3</mm-list-item>
@@ -67,17 +69,7 @@
6769
</mm-inline-box>
6870
</div>
6971

70-
<!--
71-
<div class="col" span="1">
72-
<mm-header size="medium">Header</mm-header>
73-
<mm-input fitparent placeholder="Type Here Bro"></mm-input>
74-
</div>
75-
<div class="col" span="1">
76-
<mm-header size="medium">Header</mm-header>
77-
<mm-input fitparent placeholder="Type Here Bro"></mm-input>
78-
</div>
79-
-->
80-
72+
<!-- TODO: Add a footer config, bake the footer into mm-form -->
8173
<mm-footer id="testFormFooter" unresolved fitparent semi-transparent>
8274
<mm-button id="submitBtn">
8375
<label>Save</label>
@@ -92,14 +84,14 @@
9284
testFormFooter;
9385

9486
window.addEventListener('WebComponentsReady', function(e) {
95-
87+
9688
testForm = Polymer.dom(document).querySelector('#testForm');
9789
submitBtn = Polymer.dom(document).querySelector('#submitBtn');
9890
testFormFooter = Polymer.dom(document).querySelector('#testFormFooter');
9991

10092
submitBtn.addEventListener('click', submitForm);
10193

102-
// TODO: test out in a view - think about architecture
94+
// TODO: Add a footer config, bake the footer into mm-form
10395
testForm.addEventListener('form-submit', function(e) {
10496
console.log(e.detail);
10597
if (!e.detail.isValid) {

src/mm-form/mm-form.js

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,62 +42,129 @@
4242
"_applyStyles(columns, spacing)"
4343
],
4444

45+
// common validation rules
46+
rules: {
47+
email: function(i) {
48+
var regEx = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
49+
return regEx.test(i);
50+
},
51+
alpha: function(i) {
52+
var regEx = /^\w+$/;
53+
return regEx.test(i);
54+
},
55+
int: function(i) {
56+
var regEx = /^\d+$/;
57+
return regEx.test(i);
58+
},
59+
decimal: function(i) {
60+
var regEx = /^\d*[.]\d+$/;
61+
return regEx.test(i);
62+
},
63+
whitespace: function(i) {
64+
var regEx = /\s/;
65+
return i.length > 0 && !regEx.test(i);
66+
},
67+
checked: function(i) {
68+
return i === true;
69+
},
70+
empty: function(i) {
71+
return i && i.length > 0;
72+
},
73+
blank: function(i) {
74+
return i.trim().length > 0;
75+
}
76+
},
77+
78+
// *******************************
79+
// Form Data:
80+
// {
81+
// key: { // object.getAttribute('form-id')
82+
// field: object, // the field element
83+
// value: object/string, // value of the field
84+
// validation: string, // i.e.: 'int|empty'
85+
// errorMessage: object // the error message element i.e: Polymer.dom(document)querySelector(field.getAttribute('error-message'));
86+
// },
87+
// ...
88+
// }
89+
// *******************************
90+
4591
ready: function() {
4692
// build form data object
4793
// TODO: consider effective children apis once we get to v1.2.3
4894
// https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
4995
this.async(function() {
5096
this.formItems = Polymer.dom(this).querySelectorAll('[form-id]');
5197
this.formItems.forEach(function(item) {
52-
this.formData[item.formId] = item.value;
98+
var key = item.getAttribute('form-id'),
99+
field = item,
100+
value = item.value,
101+
validation = item.getAttribute('validation'),
102+
errorMessage = Polymer.dom(this).querySelector(item.getAttribute('error-message'));
103+
104+
this.formData[key] = {
105+
'field': field,
106+
'value': value,
107+
'validation': validation,
108+
'errorMessage': errorMessage
109+
};
53110
}.bind(this));
54111
console.log("this.formData: ", this.formData);
55112
});
56113
},
57114

115+
// validate per field:
116+
_validateField: function(field) {
117+
// construct the test set based on pipes(?):
118+
var testSet = field.validation.replace(/\s/g, '').split("|")
119+
result = result = testSet.map(function(item) {
120+
return this.rules[item](field.value);
121+
}, this).filter(function(item) {
122+
return item === true;
123+
});
124+
125+
return result.length === testSet.length;
126+
},
127+
58128
submitForm: function() {
59129
var invalid = [],
60130
valid = [];
61131

62132
// UI validation pass:
63133
// console.log('submitForm');
64134
this.formItems.forEach(function(item){
65-
// update the form data object:
66-
this.formData[item.formId] = item.value;
135+
var key = item.getAttribute('form-id'),
136+
value = item.value,
137+
dataItem = this.formData[key],
138+
isValid = false;
67139

68140
// validate UI:
69-
var isValid = item.validate(item.value);
141+
dataItem.value = value;
142+
isValid = this._validateField(dataItem);
70143

71-
// track invalids
144+
// track invalid & valid fields
72145
if (!isValid) {
73-
invalid.push(item.formId);
146+
invalid.push(key);
147+
dataItem.errorMessage.style.display = 'block';
74148
} else {
75-
valid.push(item.formId);
149+
valid.push(key);
150+
dataItem.errorMessage.style.display = 'none';
76151
}
77152

78153
// show error state:
79154
item.error = !isValid;
80155

81-
// show target message:
82-
if(item._errorTarget && !isValid) {
83-
// item._errorTarget.display();
84-
item._errorTarget.style.display = 'block';
85-
} else {
86-
// item._errorTarget.hide();
87-
item._errorTarget.style.display = 'none';
88-
}
89156
}.bind(this));
90157

158+
// TODO:
91159
if (invalid.length > 0) {
92160
// there were errors
93161
// show footer error
94-
// console.log('invalids:', invalids);
95-
this.submitMessage = 'This form contains errors';
96162
} else {
97163
// send the data to some endpoint
98164
// handle that response
99165
}
100166

167+
// TODO - footer logic in here not index:
101168
// fire an invalid form event:
102169
this.fire('form-submit', {
103170
isValid: !invalid.length > 0,

src/shared/behaviors/formable.html

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
observer: "_formIdChanged",
2121
reflectToAttribute: true
2222
},
23-
// errorMessage: {
24-
// type: String,
25-
// value: "you done messed up, now what'cha gon do?",
26-
// obsrver: "_errorMessageChanged"
27-
// },
2823
errorTarget: {
2924
type: Object,
3025
observer: "_errorTargetChanged"
@@ -48,12 +43,6 @@
4843
});
4944
}
5045
},
51-
52-
// _errorMessageChanged: function(newVal, oldVal) {
53-
// this.errorTarget.message = newVal;
54-
// },
55-
56-
//
5746
};
5847

5948
scope.Formable = [

0 commit comments

Comments
 (0)