sha256 / Pristine

Vanilla javascript form validation micro-library
https://pristine.js.org/
MIT License
410 stars 81 forks source link

Can't get to work addError() #57

Open 8lall0 opened 3 years ago

8lall0 commented 3 years ago

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!

essentin commented 3 years ago

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");
8lall0 commented 3 years ago

Thanks, it works!

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

essentin commented 3 years ago

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