Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined error message #127

Open
artemchiruhin opened this issue Aug 21, 2023 · 4 comments
Open

Undefined error message #127

artemchiruhin opened this issue Aug 21, 2023 · 4 comments

Comments

@artemchiruhin
Copy link

So, when I try to show errors messages from backend under input after AJAX request by using showErrors method, it renders message correctly, but then I change any input in form and this message changes to "undefined" like string. I found a solution in using resresh method before showErrors, but should it work like that?

@horprogs
Copy link
Owner

@artemchiruhin
Copy link
Author

I have this code in registration form class for validation

this.validator
  .addField('#phone-input', [
      {
          rule: 'required',
          errorMessage: 'Поле обязательно для заполнения',
      },
      {
          rule: 'customRegexp',
          value: /^\+7\(\d{3}\)\d{3}-\d{2}-\d{2}$/,
          errorMessage: 'Формат телефона не корректный'
      }
  ])
  .onSuccess(() => this._register());

In register method I make AJAX request and render errors from backend like this

data.errors.forEach(error => {
      this.validator.showErrors({
          [`#${error.customData.field}`]: error.message
      });
  });

But frontend validation breaks
Video - https://skrinshoter.ru/vLTZ2ScVlcm?a

@horprogs
Copy link
Owner

It's better to call showErrors() just once, instead of doing it in a loop. Try this:

const errors = data.errors.reduce((acc, error) => {
    acc[`#${error.customData.field}`] = error.message
    return acc
  }, {})
  this.validator.showErrors(errors)

@artemchiruhin
Copy link
Author

It does not work, only calling refresh method before showing errors helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants