-
-
Notifications
You must be signed in to change notification settings - Fork 541
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
Comments
As it is currently in Flutter, there's no way of passing up an error to a FormField. Problem is currently, I'm a bit short on time. |
Thanks @danvick 😄 |
Maybe I can help you, I'm short on time too. But if I can help, tell me. |
@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! |
Hi @RodolfoSilva, |
Ok @danvick , I'll check later :D |
I'm marking this as a duplicate of #123. |
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?
The text was updated successfully, but these errors were encountered: