1

I'm trying to render the error messages coming back from the server on a failed form submission using vue.js and axios.

COMPONENT:

<template>
  <div>
    <p>Sign Up</p>

    <form v-on:submit.prevent="onSubmit">
      <div>
        <label>Email:</label>
        <input type="text" name="user[email]" ref="user_email" />
      </div>
      <div>
        <label>Password:</label>
        <input type="text" name="user[password]" ref="user_password" />
      </div>
      <div>
        <input type="submit"/>
      </div>
    </form>

    <ul v-if="errors && errors.length">
        {{ errors }}
    </ul>
  </div>
</template>

<script>
  import axios from 'axios'

  export default {
    data() {
      return {
        errors: []
      }
    },
    methods: {
      onSubmit: function () {
        axios.post('/users', {
          user: {
            email: this.$refs.user_email.value,
            password: this.$refs.user_password.value
          }
        })
        .then(response => {
        })
        .catch(error => {
          console.log(error.response.data.errors)
          console.log(error.response.status)
          this.errors = error.response.data.errors
        })
      }
    }
  }
</script>

CONSOLE.LOG ERRORS

{email: Array(1), password: Array(1)}
422

As you can see I'm trying to render the {{ errors }} object just to get started but no errors are showing up on the page even if the server sent back some?

Just to be clear. How can I take the form validations coming back from the server and display them on the form if any exist? Using Vue.js and Axios. Thanks for the help!

4
  • What does the response from the server look like when validation fails? Commented Feb 3, 2018 at 16:27
  • Updating now. I'll post the console.log Commented Feb 3, 2018 at 16:27
  • Not sure if it already does, but your server must respond with an error HTTP status code for axios to throw an error you can catch. E.g. 400, 401, 422, 500 etc. Commented Feb 3, 2018 at 16:30
  • It responds with a 422 Commented Feb 3, 2018 at 16:32

2 Answers 2

1

Look carefully at your console log for the response data:

{email: Array(1), password: Array(1)}

Now look at your v-if directive:

<ul v-if="errors && errors.length">

As you can see in the console log, your response has no length property so the div will not show. You initialize errors to be an empty array, but your response is returning an object, not an array.

You either need to fix your server call to return an array of errors or change your v-if directive and omit the check on the length property.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure is this is the most idiomatic way of going about this, but this will at least get the message to the form:

    .catch(error => {
      this.errors.push(error.response.data.errors)
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.