flutter_form_builder
flutter_form_builder copied to clipboard
Form validation - Optional parameter with certain conditions
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
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.
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
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.
Fair enough. Something like an optional validation parameter value or a FormBuilderValidator.optional, similar to the required one could be useful.
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