I
I
Ilya Kochkin2020-10-02 22:33:20
Vue.js
Ilya Kochkin, 2020-10-02 22:33:20

How to check data before changing data in vue.js?

Good afternoon, there is an input field, how can I check the input data, if they do not pass validation, then do not change the variable?

let app = new Vue({
        el: '#productsCart',
        data: {
            quantity: 5
        },
//вочер срабатывает после того как пользователь 
        watch: {
            quantity: function (newData, oldData) {
                console.log(oldData)
            }
        },
    })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WapSter, 2020-10-02
@wapster92

Well, create a method in it, write validation, if validation passes, change the variable.

A
Alexander, 2020-10-02
Madzhugin @Suntechnic

Make two objects with data - one connected to the fields through the model, which will reflect their state, and the other validated data to send.
Here's how to keep track of data changes in the object associated with the fields and transfer it to the object for sending when they are valid.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question