logaretm / vee-validate

✅ Painless Vue forms
https://vee-validate.logaretm.com/v4
MIT License
10.8k stars 1.26k forks source link

Checking if form is dirty using ValidationProvider #2208

Closed Mikilll94 closed 5 years ago

Mikilll94 commented 5 years ago

How to check if form is dirty using ValidationProvider?

In docs it is clearly explained how to achieve that using directive

https://baianat.github.io/vee-validate/guide/flags.html#introduction

Here is the code: isFormDirty() { return Object.keys(this.fields).some(key => this.fields[key].dirty); }

Unfortunately when using ValidationProvider I noticed that this.fields is empty. So to check if form is dirty using ValidationProvider?

logaretm commented 5 years ago

Sorry about the confusion, the fields do not represent the provider components, as they have their own isolated scope.

You could add a top-level observer and get your field status off that:

<ValidationObserver v-slot="{ dirty }">
 <!-- Some Providers -->
</ValidationObserver>

You could also get it manually like this:

<ValidationObserver ref="observer">
 <!-- Some Providers -->
</ValidationObserver>
function isFormDirty() {
  return this.$refs.observer && this.$refs.observer.ctx.dirty;
}

v3 will have much clearer documentation regarding the validation components.

segadora commented 2 years ago

I am having this issue now and just for others, this.$refs.observer.ctx. is undefined in version 3.4.14