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

Version 3, is customized validation supported? #44

Closed
jasonlaw opened this issue Apr 5, 2019 · 6 comments
Closed

Version 3, is customized validation supported? #44

jasonlaw opened this issue Apr 5, 2019 · 6 comments

Comments

@jasonlaw
Copy link

jasonlaw commented Apr 5, 2019

Hi @danvick , does version 3 still support for custom validator?
Suppose I have a text field with some customize validation, can it still be done now?

@danvick
Copy link
Collaborator

danvick commented Apr 5, 2019

Ooh yeah. The fields have an attribute named validators which takes a list of FormFieldValidators. That makes validators composable and reusable. There is a class named FormBuilderValidators with all the common validators but you can definitely include your own custom validator.
Checkout the v3 example (on pub).
Sample Usage:

FormBuilderTextField(
    attribute: "age",
    decoration: InputDecoration(labelText: "Age"),
    validators: [
        FormBuilderValidators.numeric(),
        FormBuilderValidators.max(70)
        FormBuilderValidators.min(18, errorText: "You can't be under 18.")
    ],
),

P.S. I'm currently working on comprehensive documentation.

@jasonlaw
Copy link
Author

jasonlaw commented Apr 5, 2019

That means we can create our own customized FormBuilderValidator right?

@danvick
Copy link
Collaborator

danvick commented Apr 5, 2019

Sure. Or alternatively just stick in your validator function inside the list of validators, like so:

FormBuilderTextField(
    attribute: "over_18",
    decoration: InputDecoration(labelText: "Are you over 18?"),
    validators: [
        FormBuilderValidators.required(),
        (val){
            if(val != "Yes")
                return "The answer must be Yes";
        },
    ],
),

@danvick
Copy link
Collaborator

danvick commented Apr 5, 2019

The validator function just has to be of type FormFieldValidator from Flutter. The reason for making it this way - in a list - is to allow you to define your custom validator once and just use it in different fields or even across multiple forms / pages.

@jasonlaw
Copy link
Author

jasonlaw commented Apr 5, 2019

I see, that's cool....great job! Thank you very much!

@danvick
Copy link
Collaborator

danvick commented Apr 5, 2019

I'm glad you appreciate it.

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