Vue-form-generator: Attaching errors to a field in the schema

Created on 12 Oct 2018  路  4Comments  路  Source: vue-generators/vue-form-generator

I'm submitting a ...

  • [] bug report
  • [鉁擼 feature request
  • [] support request => Please do not submit support request here, see note at the top of this template.

Hi

I don't know how people use this component but in my case, submitting the form is sending its data to the backend, and the backend is checking if the data submitted is correct. I know that vue-form-generator has validators, but I just can't trust only those.

So in my case, the backend is able to put the errors along the fields into the form schema.

Something like

{
    type: "input",
    inputType: "text",
    label: "Name",
    model: "name",
    errors: ['This field should be...', 'This field does not have...']
},

However, it doesn't seem that vue-form-generator has any feature to display an error message on a field if there are any included in the field data.

My first question is: is there a way to make that happen using available features in vue-form-generator? If not, how should I implement such feature? I guess I can create custom fields, but that means I would have to create custom fields for each different type I use, which is not the best.

Thanks

Most helpful comment

Another thought, using the error property of the schema, you can just check the field schema and return the error property from a custom validator as well.

All 4 comments

I鈥檇 create an async validator and just return any server side validation errors through that.

Use the built in validators to cut down on server side calls when the client side says it isn鈥檛 valid.

You can also inject errors into the VFG, don鈥檛 have the code in front of me, but I believe all the errors are stored on an errors property on the main VFG instance (may be on the new formGroup component in the latest release?). You could just modify that directly using the same format VFG uses.

Closing this issue as the feature can be implemented with a custom validator.

Another thought, using the error property of the schema, you can just check the field schema and return the error property from a custom validator as well.

Another thought, using the error property of the schema, you can just check the field schema and return the error property from a custom validator as well.

I just implemented this.

            // Display failure message, go back to form
            let errorsArray = []
            for (const fieldName in errorResponse.data.errors) {
                // Each form section has its own "errors" array
                // Add the error to the field's section
                errorsArray.push({
                  field: fieldSchema,
                  error: errorMessage
                })
              }
            }
            this.$refs['vue-form-generator'].errors = errorsArray

It worked well. Just note that each instance of <vue-form-generator /> has its own errors array

https://github.com/vue-generators/vue-form-generator/blob/721a4eebbde77040a1318e60aa9ef21229bbcf99/src/formGenerator.vue#L142
This is where I found the error array structure

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Stephen9s picture Stephen9s  路  4Comments

ndro picture ndro  路  4Comments

dflock picture dflock  路  5Comments

kiankji picture kiankji  路  4Comments

reardestani picture reardestani  路  5Comments