[Vuejs]-Vee-validate (VueJS) – evaluating a condition asynchronously

2👍

Didn’t know how to work with Promises.
Eventually got it working by extending one of the official samples:

 const customRule = {
    getMessage(field, params, data) {
      return (data && data.message) || 'Something went wrong';
    },
    validate(aValue) {

      return new Promise(resolve => {

        var formData = new FormData();
        formData.append("nameFilter", aValue);

        $.ajax({
          type: "POST",
          url: url,
          data: {
            action: "validate",
            value: aValue,
          }
        }).done(function (data) {

          if (!ok)
          {
            resolve({
              valid: false,
              data: {message: "Condition not met"}
            });
          }
          else
          {
            resolve({
              valid: !! aValue,
              data: undefined
            });
          }

        });

      });
    }
  };

Leave a comment