max validation incorrect with vm.$watch
Versions:
- VueJs: 2.0.3
- Vee-Validate: 2.0.0-beta.17
Description:
I'm tring to use vm.$watch to validate the max length of a textarea tag, but the first time when the length more than 1024, it shows no error, length goes to 1025. And I type in one more word, it shows the error twice. Do I use something wrong? Here is the core code:
in beforeMount:
this.$watch('work.description', function(newVal, oldVal) {
let result = this.errors.has('textMax');
if (result) {
this.work.description = this.work.description.substring(0, 1024);
}
});
in html:
<textarea v-validate data-vv-name="textMax" data-vv-rules="max: 1024" v-model="work.description"></textarea>
thank you.
I'm not sure what do you mean by "appearing twice" from what I can see everything seems in order except that your watch function might hide the error because it is constantly modifying it whenever it exceeds the character limit.
try to create a fiddle that produces the issue: I created one here that represents what I understood: https://jsfiddle.net/9ykwqsvh/
First of all, thank you for responsing.
The instance you create on fiddle is nearly what I mean. In order to make the test easier, I change the max to 5, and print out the validate result in console.here
when I type the 6th letter in, this.errors.has('textMax') returns false, but the html shows the error message. That's where I get confused.
Probably because the watch executes after/before the validation occurs due to them being in different callbacks, and one of them is modifying the value. in other words I think they do not see the same value. or could be something else entirely.
instead you could try to move the validation to the watch callback, which will require some changes like here:
https://jsfiddle.net/7u0z4cse/1/
as you can see I added the validate-on attribute which will validate only on blur event, so I basically removed the input event from triggering validation. then I validate the value manually using the validator in the watch callback.
Yes, that's what I need right now. You are doing a great job. Hope you can make it better and easier to use. Thank you.