question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Placeholder support in async validations

See original GitHub issue

It would be nice to have placeholder support for the attribute name inside the error message returned for the callback in an async validation. This is posible with synchronous validations right now.

Actual behavior

Validator.registerAsync(
  'unique',
  (value, args, attribute, passes) => {
    // database check
    passes(false, 'The :attribute has already been taken.'); // It doesn't work!
  }
);

Expected behavior

Validator.registerAsync(
  'unique',
  (value, args, attribute, passes) => {
    // database check
    passes(false, 'The :attribute has already been taken.'); // The username has already been taken.
  }
);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
MatiasOliveracommented, Jul 18, 2019

@haitm-eas I wrote an example showing how to register an async rule in #302.

You can generate the error message dynamically before passing it to passes function.

Validator.registerAsync(
  'unique',
  (value, args, attribute, passes) => {
    // database check
    passes(false, `The ${attribute} has already been taken.`);
  }
);

You need to pass a callback to passes function if the validator has one or more async rules. Related with “Use the same API for sync and async validations” in #303

if (validator.hasAsync) {
  validator.passes(() => {
    // success
  });
  validator.fails(() => {
    // failure
    const errors = validator.errors.all();
  });
}
1reaction
MatiasOliveracommented, Jul 23, 2019

@haitm-eas The example is showing how to make an async rule using promises, not async/await. The callback is a normal function, not an async function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Placeholder support in async validations · Issue #301 - GitHub
I want to apply an unique rule which query from DB, and generate dynamically error message. I don't understand what passes is?
Read more >
Using Custom Async Validators in Angular Reactive Forms
We are going to create a very minimalist form that does one thing: check if the username already exists. In this case, we...
Read more >
Async Change Validation Example - Redux Form
To provide asynchronous validation, provide redux-form with an asynchronous validation function ( asyncValidate ) that takes an object of form values, and the ......
Read more >
Async validator for unique userId not working - Stack Overflow
When I start entering anything the input field, it shows "Cannot read property 'existingUserId' of null". I've already followed various ...
Read more >
How to Add Async Validators to an Angular Reactive Form
Learn how you can add async validators to your reactive form in Angular.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found