flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

Form validation - Optional parameter with certain conditions

Open JaviCore opened this issue 4 years ago • 5 comments

So just like the title says I'm trying to set up a field, a string for my phone number (no, I can't really use the default field cause I have to add an "edit on/off" button and I don't need the country picker), which is optional but in case the user inputs something I need it to be a certain length. This is actually throwing an error when validating as the minLength validator checks the input even if there's no value so I was wondering if there is some kind of way to implement this besides changing validators during runtime once there's real input ( which I'm not really sure you can do). Something like FormBuilderValidators.optional would be useful.

Thanks

JaviCore avatar Jan 18 '21 06:01 JaviCore

If I understand correctly, perhaps you're looking for conditional validation.

There is a video from last year by SyntacOps that I think can help you out. https://youtu.be/7FBELQq808M?t=880

Check it out and come back if that isn't what you're looking for.

andrewzakhartchouk avatar Jan 22 '21 01:01 andrewzakhartchouk

It's a bit different. The video shows how to relate two fields when doing the validation for one. If A == X then B is required kind of thing. What I want to do is if A != null then A.length > Y. This could be solve by writing my own custom validator everytime, like in the video, but that would mean I need to write custom validators for pretty much every other field that behaves in a similar way (optional but with certain requirements in case of input). So I was wondering if there's any other way of actually implementing this with the default validators or if this could be an upgrade in the future.

Sorry for the previous message, I didn't explain myself properly.

Thanks

JaviCore avatar Jan 22 '21 03:01 JaviCore

Unfortunately the out-of-the-box validators can't do it at this point. For now custom validators is the way, at least until @danvick gives a response.

andrewzakhartchouk avatar Jan 22 '21 03:01 andrewzakhartchouk

Fair enough. Something like an optional validation parameter value or a FormBuilderValidator.optional, similar to the required one could be useful.

JaviCore avatar Jan 22 '21 04:01 JaviCore

Hi @JaviCore A minimal example code of this will be nice.

Whats validators fails to this logic? I has some fields in one project that I configured with validators different that FormBuilderValidator.required and this behavior works

Maybe is a bug or feature lack of FormBuilderValidator.minLength

deandreamatias avatar Jul 21 '22 08:07 deandreamatias