Mike Trinh
Mike Trinh

Reputation: 1303

VUE JS - Vee Validate - Remove prefix from error message

I'm trying to remove the prefix from vee-validate error message. Every error fields are returning with a prefix of The ex: The field_name is required.

I know I can change it with Custom Error Message like this.

const dict = {
    custom: {
      field_name: {
        required: 'field_name is required.'
      }
    }
};
this.$validator.localize('en', dict);

But going like this is too much since I have many fields and it's not a DRY concept.

Is there a better way of doing this?

Upvotes: 0

Views: 702

Answers (1)

Mike Trinh
Mike Trinh

Reputation: 1303

I found a better way to do it without custom message though might not work for everyone. Using OOP I'm looping through the error bag and removing the string The.

Here is how I did it.

this.$validator.validateAll().then(result => {
  if (!result) {
    for (var i = 0; i < this.errors.items.length; i++) {
      this.errors.items[i].msg = this.errors.items[i].msg.replace(/^The /, '');
    }
    return false;
  }
});

Upvotes: 1

Related Questions