Vue-formulate: Show general message below large form if validation fails

Created on 4 May 2020  路  9Comments  路  Source: wearebraid/vue-formulate

Describe the new feature you'd like
I have a large form with up to 20 fields. Some fields are required, some are not. When I hit submit, nothing happens when there are fields that do not pass the validation. However, my form is too large to see the validation messages below these individual fields.

I would like to be able to add a general message below the form to show there are validation issues for the user to fix before the form can be submitted.

I added the component below my submit button and expected that with showValidationErrors this would be automatic but alas.

Question: is this possible in the current version?

What percentage of vue-formulate users would benefit?
80%

future

Most helpful comment

@a-trost there should be error class names applied to failing inputs.

I would imagine a method that queried the DOM by the error class css selector, grabbed the offset height of the first matching element from the document and then executed a window.scrollTo() would do the trick.

All 9 comments

This is a great point @pimhooghiemstra. I can definitely see this being a use case a lot of users would run into, so making it easy absolutely falls within the purview of what we're trying to do here. From a heuristics and UX perspective, would you prefer a generalized message like "Not all the fields are complete and valid" or would you prefer listing all of the validation error messages?

Today

Also, just so you're able to move forward with your own project, there is a way to do this now that's a bit verbose, but not too bad. You can hook into the submit-raw event and perform your own validation check.

<template>
  <FormulateForm
    @submit="sendResults"
    @submit-raw="checkValidation"
    :form-errors="errors"
  >
    <FormulateInput
      name="email"
      validation="required|email"
    />
    <FormulateErrors />
    <FormulateInput type="submit" />
  </FormulateForm>
</template>

<script>
export default {
  data () {
    return {
      errors: []
    }
  }
  methods: {
    sendResults (data) {
       // Only when successful ...
    },
    async checkValidation (submission) {
       // May or may not be successful
       const hasErrors = await submission.hasValidationErrors()
       if (hasErrors) {
         this.errors.push('Not all fields have been filled out correctly')
       } else {
         this.errors = []
       }
    }
  }
}
</script>

Thanks for the swift reply @justin-schroeder! I think a message like 'Not all fields are completely valid' is enough. Listing all individual errors (again as they are already shown below the individual fields) seems a bit overkill to me.

I'll have a look at the submit-raw event.

Thanks again!

Hey @justin-schroeder, I actually have a similar use-case and that's super helpful! I'm trying to figure out a way to also find the specific field that's failing validation and scroll the user to it. I've tried submission.showErrors() and a couple other functions that submission has on it, but no luck. Is there a way to do this?

I've got a form that's more like 90 fields long, so this is a very helpful bit of UX.

Thanks for all your amazing work!

@a-trost there should be error class names applied to failing inputs.

I would imagine a method that queried the DOM by the error class css selector, grabbed the offset height of the first matching element from the document and then executed a window.scrollTo() would do the trick.

@andrew-boyd That worked perfectly! Great solution. Sometimes I forget about vanilla stuff when deep in Vue/libraries. Thanks a ton!

Ok, tagging this as targeted for the 2.4.0 release.

@justin-schroeder We noticed impact of this solution: const hasErrors = await submission.hasValidationErrors() - we have a lot of async validation methods which are calling API to check validity of specific field. So we have to use debounce there. This is an issue because when we want to check if the form has any errors then we have to call hasValidationErrors() again - duplicated API calls.

I would like to know if the form has any errors without calling API again.
Our temporary solution:

 <FormulateForm
            ref="form"
            :disabled="!isValidate"
            @validation="validate"
            @submit="registration"
          >
validate() {
      this.isValidate = !this.$refs.form.$children.some(
        (input) => typeof input.isValid !== 'undefined' && !input.isValid
      )

So we iterated on each field and their data to check if is valid or not without calling the validate method again.

@andrew-boyd you can use this method to check if some specific field has an error without query on DOM.

I have been researching how to achieve the exact thing. Thank you. Would be great to improve on the documentation as well as I think many people would encounter this. Happy to help improve on the docs.

Phew, took a little too long to get to this one, but it has finally been resolved and will be published in 2.5. There is a new invalid-message prop. The work-in-progress docs are here:

https://vueformulatecom-git-release-250.braid.vercel.app/guide/forms/#form-validation

Was this page helpful?
0 / 5 - 0 ratings

Related issues

titusdecali picture titusdecali  路  3Comments

Verhoeckx picture Verhoeckx  路  5Comments

OperKH picture OperKH  路  5Comments

danielkellyio picture danielkellyio  路  5Comments

aminimalanimal picture aminimalanimal  路  4Comments