1

I want to show this div only if errors: [] have value

<div v-show="errors.length" class="alert alert-danger">
                            <span v-for="(error, key) in errors" :key="key">
                                <li v-for="(errorItem, errorKey) in error" :key="errorKey" class="text-danger">{{errorItem}}</li>
                            </span>
                        </div>

I have create a validation for my form and polpulate errors: [] array from request i get from backend. But i want to show this div only if i have errors. How can i do this?

methods:{
  //if i request will catch error do this

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

if errors have value will look like this

errors:Object
  email:Array[1]
  name:Array[1]

1 Answer 1

1

You may use Object.keys(errors).length instead of errors.length, since errors is an object.

// pervious
<div v-show="errors.length" class="alert alert-danger">

// new
<div v-if="hasErrors" class="alert alert-danger">

computed: {
   hasErrors() {
        return Object.keys(this.errors).length > 0;
   }
}

update:

to enable Toastify() you may add the Object.keys(errors).length in catch

methods: {
    
    .catch(error => {
        this.errors = error.response.data.errors;

        if(Object.keys(error.response.data.errors).length > 0) {
            Toastify({text: error.response.data.message,...})
        }

}

(But I guess you are using requests-sender something like axios, you can add your Toastify() in the axios global interceptor)


update:

For Laravel backend, I recommend you use vform for form validations and alerts.

But if you want to handle this manually, there is a basic login form example:

your Laravel validator may look like below

return [
    'email' => 'required|email',
    'password' => 'required|string|min:8|max:32'
];

your login form via Vue2 Single-File-Component

<template>
    <form
        @submit.prevent="login">


        <input
            name="email"
            type="email"
            v-model="form.email"
            described-by="email-alert"/>
        <p
            v-if="errors.email.length"
            id="email-alert">
            <span
                v-for="error in errors.email"
                v-text="error"/>
        </p>

        <!-- You may combine the `<input/>` and `<p/>` as a Vue-Component -->
        <input
            name="password"
            type="password"
            v-model="form.password"
            described-by="password-alert"/>
        <p
            v-if="errors.password.length"
            id="password-alert">
            <span
                v-for="error in errors.password"
                v-text="error"/>
        </p>


        <button>
            login
        </button>
    </form>
</template>

<script>
import axios from 'axios';
export default {
    data() {
        return {
            form: {
                name: '',
                password: '',
            },
            errors: {
                name: [],
                password: [],
            }
        }
    },
    methods: {
        login() {
            axios.post('/authorizations', this.form)
                 .then(() => {
                     // no errors
                 })
                 .catch(error => {
                      this.errors = error.response.data.errors
                 });
        }
    }
}
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

this works. But how about shoowing errors with alert? Toastify({ text: error.response.data.message,
@leoon0012 I've updated the answer, hope it may help you
This show still only one alert. The given data was invalid i want to display for each field, who is required errors : {user_id: ["The user id field is required."], name: ["The name field is required."],…} email : ["The email field is required."] name : ["The name field is required."]
@leoon0012 It seems like you are using Laravel as you backend?
yes i am using laravel for backend
|

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.