3

I have 2 fields to validate but always returns true in the method validateBeforeSubmit() after i submit.

Whitout button submit it works nice and returns errors.first(name) in the good language

What do i do wrong?

App.vue:

<form @submit.prevent="validateBeforeSubmit">

    <form-input v-on:input="handleTitle" :validate="'required|email'" label="email" labelvalue="email" type="text" placeholder="" name="email" :value="title" classname="form-control" id=""></form-input>
    <form-input v-on:input="handleLink" :validate="'required'" label="Link" labelvalue="Link" type="text" placeholder="" name="link" :value="link" classname="form-control" id=""></form-input>

methods: {
        validateBeforeSubmit() {
            this.$validator.validateAll().then((result) => {
                if (result) {
                    // eslint-disable-next-line
                    alert('Form Submitted!');
                    return;
                }

                alert('Correct them errors!');
            });
        },

Input.vue:

<template>
     <div>
        <div class="form-group">
            <span>{{ errors.first(name) }}</span>
            <label v-if="label" :for="label" v-html="labelvalue"></label>
            <input v-validate="validate" v-on:input="updateValue($event)" :type="type" :placeholder="placeholder" :name="name" :value="value" :class="classname" :id="id">
        </div>
     </div>
</template>

export default {
    props: {
        validate: String,
        type: String,
        placeholder: String,
        name: String,
        value: String,
        classname: String,
        id: String,
        label: String,
        labelvalue: String
    },
    methods: {
        updateValue: function (evt) {
            this.$emit('input', evt)
        }
    }
}

1 Answer 1

5

validateAll does not look into the child component. You have to inject parent validator. Add inject: ['$validator'] in Input.vue file. It should solve the problem. The export block will look like this

export default {
    inject: ['$validator'],
    props: {
        validate: String,
        type: String,
        placeholder: String,
        name: String,
        value: String,
        classname: String,
        id: String,
        label: String,
        labelvalue: String
    },
    methods: {
        updateValue: function (evt) {
            this.$emit('input', evt)
        }
    }
}

For more informatin about inject you can look into this reference

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.