Pristine icon indicating copy to clipboard operation
Pristine copied to clipboard

Can't get to work addError()

Open 8lall0 opened this issue 4 years ago • 3 comments

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!

8lall0 avatar Dec 15 '20 15:12 8lall0

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");

essentin avatar Dec 16 '20 15:12 essentin

Thanks, it works!

I think that this is a bug that needs to be fixed by simply initialiting the errors array on creation.

8lall0 avatar Dec 16 '20 15:12 8lall0

Yeah, it definitely should be fixed "inside" Pristine, not like this, but for now it gets the job done 😄

essentin avatar Dec 16 '20 15:12 essentin