-
Notifications
You must be signed in to change notification settings - Fork 533
Introduce a new state for validation class. #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ div.vue-form-generator(v-if='schema != null') | |
validateAfterLoad: false, | ||
validateAfterChanged: false, | ||
validationErrorClass: "error", | ||
validationSuccessClass: "", | ||
validationSuccessClass: "success" | ||
}; | ||
} | ||
}, | ||
|
@@ -102,6 +102,7 @@ div.vue-form-generator(v-if='schema != null') | |
|
||
data () { | ||
return { | ||
touched: [], | ||
errors: [] // Validation errors | ||
}; | ||
}, | ||
|
@@ -165,22 +166,24 @@ div.vue-form-generator(v-if='schema != null') | |
// Get style classes of field | ||
getFieldRowClasses(field) { | ||
const hasErrors = this.fieldErrors(field).length > 0; | ||
const wasTouched = this.fieldTouched(field); | ||
let baseClasses = { | ||
error: hasErrors, | ||
error: hasErrors && wasTouched, | ||
success: !hasErrors && wasTouched, | ||
disabled: this.fieldDisabled(field), | ||
readonly: this.fieldReadonly(field), | ||
featured: this.fieldFeatured(field), | ||
required: this.fieldRequired(field) | ||
}; | ||
|
||
let {validationErrorClass, validationSuccessClass} = this.options; | ||
if (validationErrorClass && validationSuccessClass) { | ||
if (validationErrorClass && validationSuccessClass && wasTouched) { | ||
if (hasErrors) { | ||
baseClasses[validationErrorClass] = true; | ||
baseClasses.error = false; | ||
} | ||
else { | ||
} else { | ||
baseClasses[validationSuccessClass] = true; | ||
baseClasses.success = false; | ||
} | ||
} | ||
|
||
|
@@ -283,6 +286,12 @@ div.vue-form-generator(v-if='schema != null') | |
onFieldValidated(res, errors, field) { | ||
// Remove old errors for this field | ||
this.errors = this.errors.filter(e => e.field != field.schema); | ||
this.touched = this.touched.filter(e => e.field != field.schema); | ||
|
||
this.touched.push({ | ||
field: field.schema, | ||
touched: true | ||
}); | ||
|
||
if (!res && errors && errors.length > 0) { | ||
// Add errors with this field | ||
|
@@ -301,10 +310,16 @@ div.vue-form-generator(v-if='schema != null') | |
// Validating the model properties | ||
validate() { | ||
this.clearValidationErrors(); | ||
this.touched.splice(0); | ||
|
||
this.$children.forEach((child) => { | ||
if (isFunction(child.validate)) | ||
{ | ||
this.touched.push({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering if it's not easier to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, I started with this strategy (to have touched inside field data), but I didn't think about doing what you suggest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well the idea would be that you still have this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I understand, as I said, this was my first idea. But how do you access There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just like it's done with errors. Iterate over children mounted(){
this.$parent.addField(this)
},
destroyed() {
if (this.$el && this.$el.parentNode) {
this.$el.parentNode.removeChild(this.$el);
}
this.$parent.removeField(this);
}, Parent addField(item) {
const index = this.$slots.default.indexOf(item.$vnode);
this.fieldData.splice(index, 0, item);
},
removeField(item) {
const index = this.fieldData.indexOf(item);
if (index > -1) {
this.fieldData.splice(index, 1);
}
} And you have all child info inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a special data type/class which represents a component or a tree of elements in vue Anyway that was an idea/draft which I copy pasted from one of my components so it has to be adapted to vfg implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel so dumb right now 😅 I like your solution a lot. Maybe I can introduce it only for this new state for now, and maybe move errors and other functionalities later if no problem arise ? addField(item) {
const index = this.$slots.default.indexOf(item.$vnode);
this.fieldData.splice(index, 0, item);
} into this: addField(item) {
const index = this.$children.default.indexOf(item.$vnode);
this.fieldData.splice(index, 0, item);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addField(item) {
const index = this.$children.findIndex(field=> field.$vnode == item.$vnode);
this.fieldData.splice(index, 0, item);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok so your solution doesn't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure no problem. Those were only suggestions, If they take too much time then any solution is ok |
||
field: child.schema, | ||
touched: true | ||
}); | ||
|
||
let errors = child.validate(true); | ||
errors.forEach((err) => { | ||
this.errors.push({ | ||
|
@@ -342,6 +357,10 @@ div.vue-form-generator(v-if='schema != null') | |
return res.map(item => item.error); | ||
}, | ||
|
||
fieldTouched(field) { | ||
return this.touched.find(e=>e.field == field); | ||
}, | ||
|
||
getFieldID(schema) { | ||
const idPrefix = this.options && this.options.fieldIdPrefix ? this.options.fieldIdPrefix : ""; | ||
return slugifyFormID(schema, idPrefix); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why push touched fields every time a validation occurs?
Maybe have a logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is cleaned at every validation