Modal form Validation errors persist when reopened

泄露秘密 提交于 2021-02-10 14:21:00

问题


About the problem

I am using Laravel 5.6.7, Vue.js. I have modal div which being opened and closed on button click. I type something. Validation fires. I close the modal div. Then clicking button to open it. I see that the validation messages still there.

Component Template

<template>
    <div>
        <form role="form">
            <input name="LastName" type="text" ref="LastName" v-validate 
                                    data-vv-rules="required" v-model="createForm.LastName">

            <p v-if="errors.has('LastName')">{{ errors.first('LastName') }}</p>

            <button v-else type="button" @click="validateBeforeSubmit()">
                Create
            </button>
        </form>
    </div>
</template>

Component Script

<script>
    export default {
        data() {
            return {
                createForm: {
                    LastName: ''
                }
            };
        },
        created() {
            this.InitializeForm();
        },
        methods: {
            InitializeForm() {
                this.createForm.LastName = "";                
            },
            validateBeforeSubmit() {
                this.$validator.validateAll();
            }
        }
    }
</script>

My findings

if you check the input type text above, I added ref attribute. Tried the below code to set the value to false for aria-invalid attribute.

this.$refs.LastName.setAttribute("aria-invalid", "false");

It sets the attribute value but validation errors are still there. Is there any proper way to get rid of workarounds like above?

I think, when I set the first value or I click on it...some attribute value is being set and due to that form errors occur.


回答1:


Assuming that you are using vee-validate,

To clear all errors,

this.$validator.errors.clear();

To clear 1 single error only,

this.$validator.errors.remove('LastName');

Add 1 of the code above to the modal close event listener and the error would be gone the next time you opened it..



来源:https://stackoverflow.com/questions/49571123/modal-form-validation-errors-persist-when-reopened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!