Pristine
Pristine copied to clipboard
Can't get to work addError()
const a = form.querySelector('input[name="email"]'); pristine.addError(a, 'asd');
It gives me:
Uncaught TypeError: Cannot read property 'push' of undefined at f.addError (pristine.min.js:1)
How should i use addError()?
Thanks!
Got stuck on same problem. But there is a (dirty) solution:
Problem
Pristine
creates an object property on input
that you pass as addError
argument. That object has errors
array, or at least it should have, but it's not initialized.
Solution
Manually initialize errors
array before using addError
Sample implementation
const a = form.querySelector('input[name="email"]');
if (a?.pristine?.errors === undefined) a.pristine.errors = [];
pristine.addError(a, "asd");
Thanks, it works!
I think that this is a bug that needs to be fixed by simply initialiting the errors array on creation.
Yeah, it definitely should be fixed "inside" Pristine, not like this, but for now it gets the job done 😄