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
feat(validator): allow to use custom validator #16
Conversation
In your example you use the PhoneParser with the phoneNumberType. I assume that's just an example you picked to be explicit. I'm not against this feature however I'm still wondering what's the use case where you'd not want the validation to happen by this lib. I can see one:
In which case it should be fixed. |
Not only, in my case I want 2 errors messages depending on invalid or not expected type. I picked up this from my real project :) |
I see the need. I've one question: would it make sens to use a |
174d51a
to
93cc763
Compare
I understand the goal of the One last thing out of focus: if |
Another common use case is to set this field required as well as using a phone number validator. You can even expose a validator class, so that it can used as below:
This way, end user may use multiple validators depending on the use case and define a specific message for each type if wanted. Inspired by flutter_form_builder validation The |
I've done the changes PARTIALLY :
This may be confusing to have
In both case above, I suppose it makes sense to always add an invalid validator when user does not explicitly set a validator. IMHO I think that solution 2 is more reliable : less conditions to test, less tests to write, less complexity, so less code and less bugs. (Keep in mind, that solution 2 is however a BC break change) Any ideas, recommandations before I perform these latests changes ? |
2a3ebbd
to
bc49db9
Compare
In your last message, are you using the word For example here
I've not yet checked the code but I like the idea of a validator interface and the user can give a new one (deprecation of old fields), because it would allow for compose method like in your example, which is an easy to read API imo. |
It's a mistake, should be |
I'm not against breaking changes as this library is kind of young it's better to do those changes now, with deprecation when possible is the way to go. So all in all I'd go with option 2 maybe with a default that depends on the provided phoneNumberType, errorTxt for deprecation. However making the |
My initial use case was to have 2 distincts message for invalid & type not allowed. But with your remark, I think you're right, and exposing an invalid validator, with an optional type may be sufficient, just a bit less explicit. Moreover, my original use case is still available like this:
Is this what you had in mind ?
My post wasn't clear about that, suggested solution 2 is to completely remove the |
I would like to deprecate the phoneNumberType property to completely remove it in a future release. Or alternatively, we could completely remove it now but that introduce a breaking change without warning. For a library as young as this one I'm not sure it makes sens to deprecate (as there is not a massive amount of active user) but it's recommended nevertheless. |
I will update this way. Good practices are good practices, whatever amount of users ;)
Sorry for multiple questions in one, as I will be offline in a moment for the next few hours and will try to finish this tonight, this might be helpful for me to have your opinion when I will get back so I can make the changes in the expected way ;) |
Yes, it is now the responsibility of the validator.
In my opinion if the validation is explained in the documentation and the changelog is explicit, then a migration guide is not necessary.
Well I'm not sure and I was not sure when writing it either. I hoped the discussion would bring us toward a decision. If we consider the use cases people generally have I'd say they generally want any phone number or a mobile phone only. In that case maybe |
Totally agree. Naming is hard and so important. Will update this way However, I don't totally agree with your last assumption about required & composed validators as I have exactly this specific use case:
Moreover, I think that as this is a very simple feature to implement, it's worth providing an easy way to achieve such needs even if majority of them would probably use only one validator without using composed validator. |
Wouldn't this work ?
I just think |
I've refactor code according to your advices. PhoneValidator.required(context, ...),
PhoneValidator.invalid(context, ...)
PhoneValidator.invalidType(context, expectedType, ...)
// convenience method for PhoneValidator.invalidType(context, PhoneNumberType.mobile, ...)
PhoneValidator.invalidMobile(context, ...)
// convenience method for PhoneValidator.invalidType(context, PhoneNumberType.fixedLine, ...)
PhoneValidator.invalidFixedLine(context, ...) Can you please validate the default message in English so I can make the translations for others languages ? It's quite a repetitive task to translate in all supported languages, so I would like to avoid doing it twice as possible ;) I have another interrogation about deprecating
But as it seems to be like a bug on previous version (ie: the validator returned value was ignored due to the usage of |
Sorry, for some reason I did not see your last message: For your other PR I made a change to the translation files which are now .arb files and need to be generated with the command - In any case I believe that the property names should be the same whether we use a localization strategy or another. Therefor the properties should look like this (camelCased):
For the error text issue, tbh I did not understand exactly what you were saying. Does the field not become invalid when the errorText is not provided by the user ? If the field is marked as invalid when it is invalid I don't see an issue with a BC especially if it's a bug. I'm eager to merge this PR and the other one as the library seems to be getting some traction currently. So ask me if help is needed somewhere. I have updates of my own in the making also. |
Sorry for the delay, this was a busy week.
Here's an example of a previous code that will be affected: BasePhoneFormField(
validator: (...) {
if (/* check for invalid */) {
return 'fake message not expected to be used as widget.errorText '
'was displayed (validator output was ignored)';
}
}
//errorText not defined
) With the above example :
|
Hmmm, due to the recently introduced localization changes (from About new translations, 2 solutions proposed:
In any case, I would really appreciate if you can share the tool used (if you have one ;)) as my original idea of using deepl seems to be impossible as it does not support all the languages presents in this package. Thanks ! |
Yeah just go ahead and remove the unnecessary code, let's keep things simple. For the translation, When I did it the first time for all countries I used google translation. However I'm don't recall how I managed to keep the keys untranslated. Maybe I wrote a short script in the browser console ? I don't know. It's not as long as it sounds though, it's just translating from english and switching through the languages and copy pasting. I can do it if needed. |
I don't think translations keys are problematics as they are not reals words anymore. At least in deepl, they aren't translated. FYI, at work, according to our team mates who are native from others countries, we use deepl because they find it more efficient and it produces less word by word translations than google translations. |
306a778
to
805629a
Compare
Ouch, I don't think the add-favorites feature should be included here. I've rebased on Take a look and tell me if you want me to rebase/squash for a proper git history and/or separated features branches. I've also updated CHANGELOG & README, don't hesitate to reword for better comprehension/english ;) |
Ho damn, yeah I might have started from your other branch for that fix to see if it fixed your issue. As you might have seen I'm not a git history maniac, quite the contrary. Previously as lead dev I received some slack about that from my team, so eventually someone else dealt with it. However since this project is growing in size of contributors it will become increasingly important to have a standard clean git strategy. So for future PR I'll adhere to the standard of squashing for pull request and not mixing things up. Concerning this one, the git history here just has too many files to do a clean review. If you already rebased, fixed the comments and the test pass I'm okay with either (merging this as is or merge before your rebase). Whatever gets the result done faster, as I'm awaiting for those 2 PR to incorporate some changes. What I propose though, is merging the other merge request first. The one with validation (I merged it on a separate branch in order to create the flutter issue) but it eventually needs to be merged in dev. That flutter issue has been reproduced by the flutter team btw. |
I can merge my last changes done yesterday about favorites countries in this one (I've added few tests), resolve conflicts with current dev branch and ensure that all tests are passing so you have only this PR to accept. Is this ok for you ? |
Yes that would be perfect. Then a new major version could be released beginning of next week. I've a breaking change I need to introduce as well and now is a good time. What about the validators though ? We merge it first or you plan on just submitting this one with both (which is fine for me, as long as we keep the history clean going forward) ? In any case those two features are not likely to be reverted so I don't think that's going to be an issue. |
* add PhoneFormField "validator" property * [BC] remove PhoneFormField "errorText" & "phoneNumberType" * [BC] remove BasePhoneFormField "errorText" * add PhoneValidator class with static methods to customize how phonenumber is validated and the related error messages * add defaults translations strings for supported locales * add tests * update README * update CHANGELOG * [example] refactor code according to above change * [example] add required option
I just notice that the |
Before PhoneFormField was a No the refactor does not have to be in the upcoming version but it has some neat fixes in it though. I'm still trying to fix a bug that somehow pass unit test but not manual testing. I don't think the validation you made is useless though, it just has to be displaced onto PhoneFormField, no? |
I miss the fact that it has been swapped between |
805629a
to
8d1df08
Compare
I don't understand the produced diff, a lots of files are fully modified but contents is the same. Did you change formatting ? spaces/tabs ? |
I have merged the changes from this branch onto my other branch. There is just 2 files with errors regarding validation. I ve done it on that branch The main issue is specifying a default validator when no context is available. In other words I'm having trouble with this line:
and about the same one in the test |
When I switch onto |
there should be 5 now (related to what I mentioned previously). As far as I understand, I'm not sure the translation can live inside the Validators side. Which is not a bad thing (separation of concerns).
This is not doable where you do not have access to Context (FormField constructor) |
Got 6 ;) but I'm still on flutter 2.2.3 because my work app cannot be upgraded until all dependencies have been updated. But this can be set to null in constructor and used later, that's what's I've done I think. Not for context, but validator is not a constant |
Got 5 errors too once flutter upgraded to 2.5.1 |
I notice that you have removed usage of the light parser in the validator class, but not the argument Noticed too, I forgot to remove a comment I made to validate a test ( |
That is a missed point. light parser is not used anymore. Actually in the way it had no effect. |
https://gitter.im/cedvdb/phone_form_field I've created a gitter for discussion |
PR Type
New feature
BC Break
Yes, see CHANGELOG file
Description
Allow end user to define a custom validator
Sample