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

Async validation #219

Closed
RodolfoSilva opened this issue Jan 16, 2020 · 7 comments
Closed

Async validation #219

RodolfoSilva opened this issue Jan 16, 2020 · 7 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Milestone

Comments

@RodolfoSilva
Copy link
Contributor

A few times after sending data to the backend, the server will respond with backend validation. How can I set an error message for for a specific field?

@danvick do you have any suggestion how can I do that?

@danvick
Copy link
Collaborator

danvick commented Jan 16, 2020

As it is currently in Flutter, there's no way of passing up an error to a FormField.
I've been working on the next major version of the package with this as one of the features.

Problem is currently, I'm a bit short on time.

@RodolfoSilva
Copy link
Contributor Author

Thanks @danvick 😄

@RodolfoSilva
Copy link
Contributor Author

Maybe I can help you, I'm short on time too. But if I can help, tell me.

@ronaldcurtis
Copy link

ronaldcurtis commented Apr 3, 2020

@RodolfoSilva Just a heads up that I have found a work-around for async validation, inspired by this article: https://medium.com/@nocnoc/the-secret-to-async-validation-on-flutter-forms-4b273c667c03

Basically, what I did was (condensed code):

class _EmailPasswordFormState extends State<EmailPasswordForm> {
  final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
 
  // We update this boolean after our async function runs
  bool _emailHasBeenTaken = false;

  // Create this custom validator that references the above variable
  FormFieldValidator validateEmailIsUnique() {
    return (val) => _emailHasBeenTaken ? "This email has been taken" : null;
  }

// ...Skipping down to where we add validators:
validators: [
  FormBuilderValidators.required(),
  FormBuilderValidators.email(),
  // Add your validator function
  validateEmailIsUnique(),
],
        
// ...Skipping down to where we submit the form:
onPressed: () {
  // Run sync validations first. If all good, then call our async function.
  if (_fbKey.currentState.saveAndValidate()) {
    // Emulating an async call here:
    Future.delayed(Duration(seconds: 1), () {
      setState(() {
        // Pretend the response says email is not unique:
        _emailHasBeenTaken = true;
      });
      
      // Re-run the validations to show the error message
      _fbKey.currentState.saveAndValidate();
    });
  }
}

I hope that helps!

@danvick danvick added the enhancement New feature or request label May 7, 2020
@danvick danvick added this to the v4.0.0 milestone Jul 5, 2020
@danvick
Copy link
Collaborator

danvick commented Jul 7, 2020

Maybe I can help you, I'm short on time too. But if I can help, tell me.

Hi @RodolfoSilva,
I have made some progress in the v4.0.0 milestone. I may need your help in testing it out. Thanks in advance.

@RodolfoSilva
Copy link
Contributor Author

Ok @danvick , I'll check later :D

@danvick danvick added the duplicate This issue or pull request already exists label Jul 22, 2020
@danvick
Copy link
Collaborator

danvick commented Jul 22, 2020

I'm marking this as a duplicate of #123.

@danvick danvick closed this as completed Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants