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
Validation of array fields #486
Comments
You have correctly identified the situation. It's a little hacky, but you could put the "Need at least one phone" error in the form-wide No obvious solution is coming to me... 💭 |
What about adding special
btw it would be nice to have this |
If you like it I think I can implement this |
@trashgenerator @erikras I like this proposition as well 👍 That could be applied both to arrays and deep objects in the same way. What could be even better is that, if a validation (including |
Any update on this one? If think @trashgenerator solution in the best deal here... |
Also interested in a solution for this one. I'd be interested if you have ideas on how to clear the errors from the array object. We have a similar example to above where we have an array of weights. Each weight must be numeric and at least one must be 100.
So I'm assuming that when your component gets the weights array field, the array field will have an error property alongside the standard array properties like addField etc. If the issue was that the array was empty you could potentially clear the array error when an array field method like addField is called. But not so clear if the issue is that no error fields meet a particular rule like, at least one must be 100. I guess you could chain up the onChange handlers or something and always clear the array error when any child changes? |
I started having a go at implementing something for this. But ran into some confusion with the way that setErrors.js works currently. I tried adding in an
error and handling that in some of the array error handling logic but the error property I set seemed to get cut out at some point. Much debugging and I really just could not grok how setErrors is supposed to work. I was testing setting a simple error on a fairly simple form structure and it seemed like the line that set the [destKey] error property got called about four times. I found it a bit confusing how there are multiple iterations going on in the clear and main part of the function. So I decided to have a go at rewriting setErrors so maybe I could understand it better. Anyway, not sure what the point of this post is but just thought I'd share what I spent some of my weekend on...certainly not intended as a judgement on the current implementation...! https://gist.github.com/mdgbayly/2090df04cc6062759860 Maybe now that I have a version of setErrors that I understand I could have a go at adding validation of array fields and then maybe retrofit to the current setErrors implementation... |
@erikras can you give some hints where one has to start fixing this in redux-form? We currently hacked around it by implementing a custom component, which gets a separate error property and handles the touched state itself. If I have time and the code is not too complex I would try providing a pull request. |
Good news! Array validation errors in |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I have a field with list of phones. User can add them, remove and edit. There two possible types of errors here:
If I need first type of errors, I should define form fields like this
['phones']
and from my validation function I should return{phones: "You should provide at least one phone"}
If I need second type of errors, I should define fields like
['phones[]']
and should returnfrom my validation function.
The problem is that it is impossible to use both types of errors on the same fields. But it is quite common use case.
Any ideas how to solves this?
The text was updated successfully, but these errors were encountered: