-
Notifications
You must be signed in to change notification settings - Fork 89
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
RequiredValidator to flag empty lists and maps in addition to nulls and empty strings #377
Comments
Hi @laogao Thanks for the issue. I understand that you would like to validate that an array, map, or any other type of Iterable must have at least one element. You can achieve that with the current validator ValidationMessage.minLength. You can just define ValidationMessage.minLength(1) to specify there should be at least 1 item in the Iterable. This validator also takes into account FormArrays, FormGroups, Strings, any Iterable. |
In my opinion null or empty string have a different meaning from an empty collection. When I'm validating a field I want to make sure the field contains a value different from null, or in empty string (whish I consider a special case of absence of value) On the other hand When I'm handling collections I want to make sure they have at least 1 item or I could check the collection is not null no matter if it has items or not, or I can check both, not null and with items. But that is something that needs to be explicitly validated because that all depends on the context and a particular requirement. Merging both concepts in the same validator will not allow to satisfy all the possible scenarios. On the other way having them separated allows you to use one or both validators of they are needed. |
Thanks for responding. That makes perfect sense. I can also envision the possibility that this seemingly small and harmless change may well break existing user code in unexpected ways. That said, it would be nice if the code comment can reflect this more precisely. One of the problems with 'minLength(1)' is that it won't work for numbers (which is, BTW, very common in Alternatively, given that Currently this is what we've done in our own repo. Could be helpful for others as well. |
Hi @laogao, Thanks for the answer. I also agree that we should update the comments on the required validator. So I would encourage you to create a PR that updates the comment on that validator and create a new validator that will serves as an alias to the Validators.minLength(1) called Validators.isNotEmpty. Does that sound good to you? |
Hi @joanpablo, No problem. The issue I had with (If you are curious, we had I know this particular case is probably not what As things stand, I'll have to specify both Next I'll try to prepare a PR to fully reflect my thinking on this. Cheers. |
After some thought, it probably makes sense to leave |
Hi @laogao
The
If you have a FormControl of type num you might want to just use That said, I'm not sure why to create another validator that will do exactly the same as |
Hi @joanpablo
To be clear, using Where I started (simplified version)
What I intend the validator to do?
What happens if
|
Thanks a lot, @laogao for clarifying your use case, now I understand your needs, and I really appreciate the time you took to explain it. I will give it another shot to the PR and think about it. IMO I would've used different Validators depending on the type of your questions, but as you explained that seems to add complexity to your code. Thanks a lot for your support. |
Another thing I would like to know: You mentioned that using different validators depending on the type of question is complex, is that complexity due to Reactive Forms, or it is intrinsic to your project? Is there any other thing (besides this new validator you are proposing) that Reactive Forms could change or do better in order to make you use different validators depending on the question a bit easier? Thanks |
Hi @joanpablo appreciate your understanding. The problem lies in the fact that we model our online forms in terms of questions (which are in turn built on top of various reactive forms widgets). Each question is of a different type (text, dropdown, radiogroup, checkbox, rating, ranking, image, file, etc.) and can have different attributes. Some of these attibutes are common, such as "isRequired". To build the online form, we are given a spec file written in a "language" business ppl can understand and maintain. The first thing we do is mapping each question into respective reactive forms widgets according to its type. (I hope you would agree that the "type" in this context captures mainly "which form widgets to use" rather than the type of its underlying value.) Our main focus at this stage is the widget tree that contains all questions business specified, renders properly, and can respond to user interaction. Validation is a separate concern, and usually comes to the main scene at a later stage: it doesn't normally affect how the questions/widgets are rendered. To deal with validation requires a different mind set. (And I think that might also be the reason you chose to put validators in their own package, and have type signatures of Now that user can see the questions and provide answers, we need to make sure the answers they provided are valid, before submitting the form to the backend. From business ppl's perspective, all they concerned about are "questions", not "widgets". So it makes perfect business sense for questions to "require" a meaningful answer, whatever that actually means. If no meaningful answer is provided, user would be prompted with the same message along the lines of "missing meaningful answer", no matter the answer need to be a Hope this all makes sense to you. Cheers. |
Congratulations on the 15.0.0 release! I noticed there was a change of style for validators. Pushed additional changes to match the new style. Hope you are still interested in this PR, namely #380 . Cheers. |
Hi @laogao Did you see the comments I left to you in the PR? The one that I particularly concerned is using that validator for numbers, it really confuses me semantically speaking. |
Thanks for your time and comments. As I explained earlier, specifically choose to use The idea was simple and straight-forward: a built-in common validator that can require the value to be non-null and non-emtpy, regardless of the type. Currently we have the following built-in validators, and neither of them fulfills the need in its entirety:
I guess the confusion might come from the fact that initially you suggested to use I opened the issue / PR with the thinking that maybe we could improve on the current Bottom line: I can provide my custom validator in my project, for sure (and thanks for keeping the extension point open). But I do think this common validator is useful for other Cheers. |
Currently the
RequiredValidator
only handles cases of null values and empty strings. And the doc reads: "Validator that requires the control have a non-empty value.". Should we handle empty lists and maps as well? Would be happy to submit a PR if this makes sense.The text was updated successfully, but these errors were encountered: