Closed
Description
Great component, but I would suggesting having the validation process more open to the ones who use the form generator.
After reading the docs and looking in the source cod I didn't find a good way of validating my generated form exactly when I want. The only implicit way was to declare a submit
input which in many real cases doesn't have to be right bellow the inputs.
Another approach would be to declare a ref="myForm"
and call this.$refs.myForm.validate()
in my custom form submit function. This approach doesn't seem clean to me and might get ugly when you have multiple forms.
I suggest maybe adding an event which will pass the errors
array or boolean up to the parent component.
Example:
<vue-form-generator
@errors-changed="handleErrors">
</vue-form-generator>
//parent component
data:function(){
return{
isValidForm:false,
}
},
methods:{
handleErrors:function(errors){
this.isValidForm = errors.length===0;
},
myCustomSubmit:function(){
if(this.isValidForm){
//proceed with my form submission
}
}
}
Or alternatively
<vue-form-generator
@on-validate="handleValidation">
</vue-form-generator>
//parent component
data:function(){
return{
isValidForm:false,
}
},
methods:{
handleValidation(isValid){
this.isValidForm = isValid;
}
}
Necessary code change
validate() {
// console.log("Validate!", this.model);
this.clearValidationErrors();
each(this.$children, (child) => {
if (isFunction(child.validate))
{
// console.log("Validate ", child.model)
let err = child.validate();
each(err, (err) => {
this.errors.push({
field: child.schema,
error: err
});
});
}
});
let isValid = this.errors.length == 0;
this.$emit('on-validate',isValid );
return isValid;
},