vue-generators / vue-form-generator

:clipboard: A schema-based form generator component for Vue.js
MIT License
2.99k stars 531 forks source link

Custom Validation Function problem #255

Closed gishan closed 7 years ago

gishan commented 7 years ago

I am using one custom validation function for validating an input field. In that validating function, I use one API to check the input value is existing or not. so when it is existing it will show 'True', if it is not then 'False', but when I type the text in a quick it will give two three Trues or Falses. as an example if 'car', 'cara' and 'carav' are existing when I want to type 'caravan' then ill get 3 'True's can someone help me with this. I think its a problem with the plugin.

validateCompanyName(val){ 
     console.log(val);
     return axios.get(VALIDATE_DOMAIN_URL + `/${val}`)
                .then(response => {
                    // JSON responses are automatically parsed.
                    console.log(response)
                    if (response.data.exists){
                        return 'True';
                    }
                })
                .catch(e => {
                    console.log(e)
                });
    };

schema: {
fields: [{
            type: "input",
            inputType: "text",
            label: "Name",
            model: "name",
            readonly: false,
            featured: true,
            required: true,
            disabled: false,
            placeholder: "User's name",
            validator: this.ValidateCompanyName  // this is the custom function
        }
}
icebob commented 7 years ago

If you have an error, you need to return with an array with error messages. If not return with null or undefined. More info: https://icebob.gitbooks.io/vueformgenerator/content/validation/custom-validators.html

gishan commented 7 years ago

Thank you for the reply and i tried your solutions but it didn't work. can you please check this gif. i need a solution for this problem https://media.giphy.com/media/xUA7b8Ixhl7NkkZZPq/giphy.gif thank you

icebob commented 7 years ago

It's weird, but I think it is not a vfg bug. You need to handle multiple ajax requests & responses in your validator func. Try for example with lodash debounce