|
26 | 26 | reflectToAttribute: true |
27 | 27 | }, |
28 | 28 | formItems: { |
29 | | - type: Array |
| 29 | + type: Object, |
| 30 | + value: function() { return {}; } |
30 | 31 | }, |
31 | 32 | formData: { |
32 | 33 | type: Object, |
|
105 | 106 | } |
106 | 107 | }, |
107 | 108 |
|
| 109 | + _initialFormData: {}, |
| 110 | + |
108 | 111 | // ******************************* |
109 | 112 | // Form Data: |
110 | 113 | // { |
111 | 114 | // key: { // object.getAttribute('form-id') |
112 | 115 | // field: object, // the field element |
113 | 116 | // value: object/string, // value of the field |
114 | 117 | // validation: string, // i.e.: 'int|empty' |
115 | | - // errorMessage: object // the error message element i.e: Polymer.dom(document)querySelector(field.getAttribute('error-message')); |
| 118 | + // errorMsg: object // the error message element i.e: Polymer.dom(document)querySelector(field.getAttribute('error-message')); |
116 | 119 | // }, |
117 | 120 | // ... |
118 | 121 | // } |
|
123 | 126 | // TODO: consider effective children apis once we get to v1.2.3 |
124 | 127 | // https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children |
125 | 128 | this.async(function() { |
126 | | - this.formItems = Polymer.dom(this).querySelectorAll('[form-id]'); |
127 | | - this.formItems.forEach(function(item) { |
128 | | - var key = item.getAttribute('form-id'), |
129 | | - field = item, |
130 | | - value = item.value, |
131 | | - validation = item.getAttribute('validation'), |
132 | | - errorMessage = Polymer.dom(this).querySelector(item.getAttribute('error-message')); |
| 129 | + var formFields = Polymer.dom(this).querySelectorAll('[name]'); |
| 130 | + |
| 131 | + formFields.forEach(function(item) { |
| 132 | + var key = item.getAttribute('name'), |
| 133 | + field = item, |
| 134 | + label = item.getAttribute('label'), |
| 135 | + value = item.value, |
| 136 | + validation = item.getAttribute('validation'), |
| 137 | + errorMsg = item.getAttribute('error-message'), |
| 138 | + errorMsgEle = null, |
| 139 | + fieldHeaderEle = null; |
133 | 140 |
|
134 | | - this.formData[key] = { |
135 | | - 'field': field, |
136 | | - 'value': value, |
137 | | - 'validation': validation, |
138 | | - 'errorMessage': errorMessage |
| 141 | + // create the label and error message if necessary |
| 142 | + if (errorMsg) { |
| 143 | + var parentEle = Polymer.dom(item).parentNode; |
| 144 | + |
| 145 | + errorMsgEle = new Strand.FormMessage(); |
| 146 | + errorMsgEle.message = errorMsg; |
| 147 | + errorMsgEle.type = 'error'; |
| 148 | + Polymer.dom(parentEle).appendChild(errorMsgEle); |
| 149 | + } |
| 150 | + |
| 151 | + if (label) { |
| 152 | + var parentEle = Polymer.dom(item).parentNode, |
| 153 | + headerTxt = document.createTextNode(label); |
| 154 | + |
| 155 | + fieldHeaderEle = new Strand.Header(); |
| 156 | + fieldHeaderEle.size = 'medium'; |
| 157 | + Polymer.dom(fieldHeaderEle).appendChild(headerTxt); |
| 158 | + Polymer.dom(parentEle).insertBefore(fieldHeaderEle, field); |
| 159 | + } |
| 160 | + |
| 161 | + // store the form data, hold on to the initial settings |
| 162 | + // for cross reference diff later |
| 163 | + this.formData[key] = this._initialFormData[key] = value; |
| 164 | + |
| 165 | + // store the form items and related data/elements |
| 166 | + this.formItems[key] = { |
| 167 | + 'field' : field, |
| 168 | + 'validation' : validation, |
| 169 | + 'errorMsg' : errorMsg, |
| 170 | + 'errorMsgEle' : errorMsgEle, |
| 171 | + 'fieldHeaderEle' : fieldHeaderEle |
139 | 172 | }; |
| 173 | + |
140 | 174 | }.bind(this)); |
141 | 175 | console.log("this.formData: ", this.formData); |
| 176 | + console.log("this._initialFormData: ", this._initialFormData); |
| 177 | + console.log("this.formItems: ", this.formItems); |
142 | 178 | }); |
143 | 179 | }, |
144 | 180 |
|
|
162 | 198 | // ******************************* |
163 | 199 | // form validation |
164 | 200 | // validate per field: |
165 | | - _validateField: function(field) { |
166 | | - // construct the test set based on pipes(?): |
167 | | - var testSet = field.validation.replace(/\s/g, '').split("|") |
168 | | - result = result = testSet.map(function(item) { |
169 | | - return this.rules[item](field.value); |
| 201 | + _validateField: function(validation, value) { |
| 202 | + // construct the test set based on pipes: |
| 203 | + var testSet = validation.replace(/\s/g, '').split("|"), |
| 204 | + result = testSet.map(function(item) { |
| 205 | + return this.rules[item](value); |
170 | 206 | }, this).filter(function(item) { |
171 | 207 | return item === true; |
172 | 208 | }); |
|
180 | 216 |
|
181 | 217 | // UI validation pass: |
182 | 218 | // console.log('submitForm'); |
183 | | - this.formItems.forEach(function(item){ |
184 | | - var key = item.getAttribute('form-id'), |
185 | | - value = item.value, |
186 | | - dataItem = this.formData[key], |
| 219 | + for (var key in this.formData) { |
| 220 | + var item = this.formItems[key] |
| 221 | + value = item.field.value, |
| 222 | + validation = item.validation, |
187 | 223 | isValid = false; |
188 | 224 |
|
| 225 | + // set the form data: |
| 226 | + this.formData[key] = value; |
| 227 | + |
189 | 228 | // validate UI: |
190 | | - dataItem.value = value; |
191 | | - isValid = this._validateField(dataItem); |
| 229 | + isValid = this._validateField(validation, value); |
192 | 230 |
|
193 | 231 | // track invalid & valid fields |
194 | 232 | if (!isValid) { |
195 | 233 | invalid.push(key); |
196 | | - dataItem.errorMessage.style.display = 'block'; |
| 234 | + item.errorMsgEle.style.display = 'block'; |
197 | 235 | } else { |
198 | 236 | valid.push(key); |
199 | | - dataItem.errorMessage.style.display = 'none'; |
| 237 | + item.errorMsgEle.style.display = 'none'; |
200 | 238 | } |
201 | 239 |
|
202 | 240 | // show error state: |
203 | | - item.error = !isValid; |
204 | | - |
205 | | - }.bind(this)); |
| 241 | + item.field.error = !isValid; |
| 242 | + } |
206 | 243 |
|
207 | 244 | // TODO: |
208 | 245 | if (invalid.length > 0) { |
209 | | - // there were errors |
210 | | - // show footer error |
| 246 | + this.footerType = 'error'; |
| 247 | + this.footerMessage = 'This form contains errors.'; |
| 248 | + this.showFooterMessage = true; |
211 | 249 | } else { |
| 250 | + |
212 | 251 | // send the data to some endpoint |
213 | 252 | // handle that response |
| 253 | + |
| 254 | + // reconfigure based on the response - display more error messaging |
| 255 | + // or change the error messaging, etc - for if backend error wasn't |
| 256 | + // caught on the UI validation pass |
| 257 | + |
| 258 | + this.footerType = 'success'; |
| 259 | + this.footerMessage = 'This form does not contain any errors.'; |
| 260 | + this.showFooterMessage = true; |
214 | 261 | } |
215 | 262 |
|
216 | 263 | // TODO - footer logic in here not index: |
|
0 commit comments