coder
coder

Reputation: 642

How to hide error message in vue js after validation is true?

Actually my problem I am checking when a particular value is valid through v-on:input. If, the value is invalid I get the response as "invalid data" and i display the same. But, when the value becomes valid, the "invalid data" is not hiding. How to I able to do so.

My code is

  methods: {
            checkLimit: function () {
                var vm = this;
                data = {};
                if (this.no != '') data['no'] = this.no;
                $.ajax({
                    url: "doc/checkNumber",
                    method: "POST",
                    data: data,
                    dataType: "JSON",
                    success: function (e) {
                          if (e.status == true) {
                                       }
                           else{
                            console.log(e.msg);
                            vm.error = e.msg;
                            }

                    },
                });
            },
} 

So if status is false, I am showing as

<input type="text" class="form-control" v-model="orno" required="" 
v-on:input="checkLimit" maxlength="3"><p>{{error}}</p>

But when Status is true the error message is still present. How can I update based on the change?

Upvotes: 1

Views: 3105

Answers (2)

B M
B M

Reputation: 4019

Simple: in checkLimit do vm.error=null on success. Then in the template do <p v-if="error">{{error}}</p>.

Since in vue all data is reactive p will disappear when error is falsy.

EDIT: if you want to keep the p and toggle the message do as suggested in the other comment (no v-if).

Upvotes: 1

zapping
zapping

Reputation: 4116

You should set the error to '' when the e.status is true. Like this

 success: function (e) {
    if (e.status == true) {
        vm.error = '';
    } else {
       console.log(e.msg);
        vm.error = e.msg;
    }
 },

Upvotes: 2

Related Questions