Skip to content
This repository was archived by the owner on Feb 12, 2020. It is now read-only.

Commit 5bdd05f

Browse files
committed
add this.formOtherErrors() method
1 parent 31f6c17 commit 5bdd05f

File tree

7 files changed

+98
-9
lines changed

7 files changed

+98
-9
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Name attributes are required for all forms and form fields. Any element without
4646
</form>
4747
```
4848

49+
4950
### Method: formErrors()
5051

5152
This method can be used to retrieve all errors for a given form, or an error for a single form field.
@@ -66,12 +67,35 @@ When `fieldName` is passed, the method will return the error corresponding to th
6667
#### Example
6768

6869
```html
69-
<div v-if="formErrors('myForm', 'myField')" class="error">
70+
<div v-if="false !== formErrors('myForm', 'myField')" class="error">
7071
{{ formErrors('myForm', 'myField') }}
7172
</div>
7273
```
7374

7475

76+
### Method: formOtherErrors()
77+
78+
This method returns any errors *not* corresponding to field names, or `FALSE` if there are none.
79+
80+
#### Arguments
81+
82+
| Type | Name | Description |
83+
| ---- | ---- | ----------- |
84+
| *string* | formName | The name of the form. |
85+
86+
#### Returns
87+
88+
This method returns any errors *not* corresponding to field names, or `FALSE` if there are none.
89+
90+
#### Example
91+
92+
```html
93+
<div v-if="false !== formOtherErrors('myForm')" v-for="(error, key) in formOtherErrors('myForm')" class="error">
94+
{{ key }}: {{ error }}
95+
</div>
96+
```
97+
98+
7599
### Method: setFormErrors()
76100

77101
This method can be used to set arbitrary form errors. The errors do not necessarily need to correspond to a field, but the form must be a valid `v-form` element.
@@ -97,6 +121,7 @@ var errors = {
97121
this.setFormErrors('myForm', errors);
98122
```
99123

124+
100125
### Method: formTouched()
101126

102127
This method can be used to determine whether or not a form or a specific field has been "touched".
@@ -122,16 +147,19 @@ When `fieldName` is passed, `TRUE` or `FALSE` are returned based on the state of
122147
<input type="text" v-bind:class="{ touched: formTouched('myForm', 'myField') }" name="myField" />
123148
```
124149

150+
125151
### Method: formChanged()
126152

127153
This method works just like [formTouched()](#method-formtouched), but says whether or not field values have changed since the form was first initialized.
128154

155+
129156
### Method: formValid()
130157

131158
This method works just like [formTouched()](#method-formtouched), but says whether or not field values are valid.
132159

133160
Note: during automatic validation, if a field is required _and_ empty, it will only trigger an invalid state _if_ it has been touched. Because of this, it is recommended you call the [validateForm()](#method-validateform) method prior to submission, which will force-touch all fields and rerun validation accordingly.
134161

162+
135163
### Method: validateForm()
136164

137165
This method force-touches all form fields, reruns validation, and returns `TRUE` if everything is happy, or `FALSE` if one or more fields are sad.
@@ -166,6 +194,7 @@ submitForm = function() {
166194
}
167195
```
168196

197+
169198
### Custom Validation Callbacks
170199

171200
Aside from the standard [Constraints](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation) like `required` and `minlength` and whatnot, you can also specify any arbitrary Vue callback function by adding `data-validation-callback="myVueMethod"` to a field.

demo/assets/demo.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/assets/vue-blob-forms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/index.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,24 @@ <h1>vue-blob-forms</h1>
317317
</div>
318318
</td>
319319
</tr>
320+
<tr v-if="false !== formOtherErrors('demo')">
321+
<th scope="row">Non-Field Errors</th>
322+
<td colspan="4"></td>
323+
<td>
324+
<div class="error-message" v-for="(error, field) in formOtherErrors('demo')">
325+
<strong>{{ field }}:</strong><br>
326+
{{ error }}
327+
</div>
328+
</td>
329+
</tr>
320330
</tfoot>
321331
</table>
322332
</form>
323333

324334
<div class="demo--actions">
325335
<button v-on:click.prevent="validateForm('demo')" class="fg-white bg-pink bg-purple-hover">Check All</button>
326336
<button v-on:click.prevent="showingToggle = !showingToggle" class="fg-white bg-blue bg-purple-hover">Toggle Field</button>
337+
<button v-on:click.prevent="otherError" class="fg-white bg-blue bg-purple-hover">Non-field Error</button>
327338
</div>
328339
</div><!--#vue-app-->
329340

@@ -334,4 +345,4 @@ <h1>vue-blob-forms</h1>
334345
<script src="assets/demo.min.js"></script>
335346

336347
</body>
337-
</html>
348+
</html>

dist/vue-blob-forms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/demo.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ var app = new Vue({
4747
}
4848

4949
return 'A valid Beatles movie title is required.';
50-
}
50+
},
51+
// Arbitrary error.
52+
otherError: function() {
53+
this.setFormErrors('demo', {miscError: 'This is a random non-field error.'});
54+
},
5155
},
5256
});
5357

54-
})();
58+
})();

src/js/vue-blob-forms.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* This is a Vue plugin for providing form validation.
55
*
6-
* @version 0.2.0
6+
* @version 0.2.1
77
* @author Blobfolio, LLC <[email protected]>
88
* @package vue-blob-forms
99
* @license WTFPL <http://www.wtfpl.net>
@@ -388,6 +388,51 @@
388388
return errors;
389389
};
390390

391+
/**
392+
* Unattached Form Errors
393+
*
394+
* Return any form errors whose keys do not directly correspond
395+
* to a field name.
396+
*
397+
* @param string Form name.
398+
* @return mixed Errors or false.
399+
*/
400+
Vue.prototype.formOtherErrors = function(name) {
401+
// Make sure the form is valid.
402+
if (!name || (typeof this.blobForms[name] === 'undefined')) {
403+
return false;
404+
}
405+
406+
var errors = this.blobErrors[name] || {},
407+
errorKeys = Object.keys(errors);
408+
409+
// We have no errors.
410+
if (!errorKeys.length) {
411+
return false;
412+
}
413+
414+
var fields = _getFields(this.blobForms[name].el),
415+
fieldKeys = [],
416+
i;
417+
for (i=0; i<fields.length; i++) {
418+
var fieldName = fields[i].getAttribute('name') || '';
419+
if (fieldName) {
420+
fieldKeys.push(fieldName);
421+
}
422+
}
423+
424+
var out = {};
425+
var found = false;
426+
for (i = 0; i < errorKeys.length; i++) {
427+
if (fieldKeys.indexOf(errorKeys[i]) === -1) {
428+
out[errorKeys[i]] = errors[errorKeys[i]];
429+
found = true;
430+
}
431+
}
432+
433+
return found ? out : false;
434+
};
435+
391436
/**
392437
* Form/Field Touched
393438
*
@@ -1525,4 +1570,4 @@
15251570
window.Vue.use(BlobFormsVue);
15261571
}
15271572

1528-
})();
1573+
})();

0 commit comments

Comments
 (0)