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

Commit e1b04de

Browse files
committed
setFormErrors() method
1 parent b7eaabb commit e1b04de

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ When `fieldName` is passed, the method will return the error corresponding to th
7171
</div>
7272
```
7373

74+
75+
### Method: setFormErrors()
76+
77+
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.
78+
79+
#### Arguments
80+
81+
| Type | Name | Description |
82+
| ---- | ---- | ----------- |
83+
| *string* | formName | The name of the form. |
84+
| *object* | errors | An object with keys relating to fields or error codes, and values being errors strings. |
85+
86+
#### Returns
87+
88+
This method will update the form errors and return `TRUE`, or `FALSE` if bad arguments were passed.
89+
90+
#### Example
91+
92+
```javascript
93+
var errors = {
94+
fieldOne: 'This is all wrong.',
95+
fieldTwo: 'This too is not correct.',
96+
};
97+
this.setFormErrors('myForm', errors);
98+
```
99+
74100
### Method: formTouched()
75101

76102
This method can be used to determine whether or not a form or a specific field has been "touched".
@@ -130,7 +156,7 @@ Note: `FALSE` is also returned if the form name is invalid or not bound to the `
130156
</form>
131157
```
132158

133-
```js
159+
```javascript
134160
submitForm = function() {
135161
if (!this.validateForm('myForm')) {
136162
return false;
@@ -154,7 +180,7 @@ If the value is valid, the function should return `TRUE`, otherwise it should re
154180
<input type="text" data-custom-callback="validateUsername" />
155181
```
156182

157-
```js
183+
```javascript
158184
validateUsername = function(value) {
159185
if (this.users.indexOf(value) !== -1) {
160186
return true;

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.

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/vue-blob-forms.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,84 @@
592592
return errorKeys.length ? errors : true;
593593
};
594594

595+
/**
596+
* Set Errors
597+
*
598+
* Pass arbitrary errors to a form. These do not necessarily
599+
* have to correspond to fields, but they must correspond to
600+
* a v-form.
601+
*
602+
* @param string $name Form name.
603+
* @param object $errors Errors.
604+
* @return bool True/false.
605+
*/
606+
Vue.prototype.setFormErrors = function(name, errors) {
607+
// Make sure the form is valid.
608+
if (!name || (typeof this.blobErrors[name] === 'undefined')) {
609+
return false;
610+
}
611+
612+
// This should be a keyed object.
613+
if (
614+
(typeof errors !== 'object') ||
615+
(errors === null) ||
616+
Array.isArray(errors)
617+
) {
618+
return false;
619+
}
620+
621+
var fields = Object.keys(this.blobFields[name]),
622+
keys = Object.keys(errors),
623+
changed = false;
624+
625+
// Again, exit if the data is bad.
626+
if (!keys.length) {
627+
return false;
628+
}
629+
630+
// Loop through everything!
631+
for (i=0; i<keys.length; i++) {
632+
var key = keys[i],
633+
value = errors[keys[i]];
634+
635+
// Bad error?
636+
if (!value || (typeof value !== 'string')) {
637+
continue;
638+
}
639+
640+
changed = true;
641+
642+
// Record the main error.
643+
this.blobErrors[name][key] = value;
644+
645+
// If this is a field, we should also update its
646+
// validity details.
647+
if (fields.indexOf(key) !== -1) {
648+
// Update internal meta.
649+
if (this.blobFields[name][key].valid) {
650+
Vue.set(this.blobFields[name][key], 'valid', false);
651+
}
652+
if (this.blobFields[name][key].el.classList.contains('is-valid')) {
653+
this.blobFields[name][key].el.classList.remove('is-valid');
654+
}
655+
if (!this.blobFields[name][key].el.classList.contains('is-invalid')) {
656+
this.blobFields[name][key].el.classList.add('is-invalid');
657+
}
658+
659+
// And for good measure, the field's constraint error.
660+
this.blobFields[name][key].el.setCustomValidity(value);
661+
}
662+
}
663+
664+
// If we made changes, update the form classes.
665+
if (changed) {
666+
this.$forceUpdate();
667+
_updateFormClasses(this.blobForms[name].el);
668+
}
669+
670+
return true;
671+
};
672+
595673
/**
596674
* Gravatar URL
597675
*
@@ -677,6 +755,11 @@
677755
// Cast $touch to a boolean.
678756
touch = !!touch;
679757

758+
// Before we start validating, let's reset the error holder
759+
// to clear any arbitrary messages a user might have
760+
// injected.
761+
this.blobErrors[name] = {};
762+
680763
// Loop through and validate each field.
681764
var fieldKeys = Object.keys($scope[name].blobFields[name]);
682765
for (i=0; i<fieldKeys.length; i++) {

0 commit comments

Comments
 (0)