-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
RA v4 blocks validation due to empty array #7500
Comments
Reproduced. Thanks! |
Just discovered that this also happens for empty objects too, not just arrays. This wasn't a problem in RA v3 |
The return type of the validator must be a flat object. When using array inputs, you must add an identifier matching the field name, e.g. {
title: 'title too short',
'backlinks.0.url': 'url too short',
} instead of {
title: 'title too short',
backlinks: [{ url: 'url too short' }],
} So your validator should be written as follows on v4: const validator = record => {
const errors = {};
record.backlinks?.forEach((val, i) => {
if (val.url.length < 2) {
errors[`backlinks.${i}.url`] = 'The backlink is not long enough';
}
});
return errors;
}; I'll check further to see if this is indeed a regression, in which case we should mention in in the Upgrade guide. |
I see no mention of the object syntax for the error object in the react-admin v3 documentation. Where did you see it documented? |
@fzaninotto, I don't think @andrico1234 read it anywhere, he is just implying that react-final-form allowed this "undocumented" behavior, and now react-admin v4 doesn't. |
Right: it's in the react-final-form documentation:
So I see two options:
WDYT? In any case, the Validation documentation should mention the fact that the return object should be flat, and give the example of an embedded array input. |
Looks like option number 2 is better. |
yep, I agree. I'll try it in the next 5 minutes. |
OK, I didn't manage to do it in 5 minutes, so I'll let it open for anyone willing to try. The solution probably comes from lodash/lodash#2240. |
What you were expecting:
Before RA v4, returning an empty array from the validator meant that RA treated the form as being valid. After the new update it seems that this is no longer the case, and we get a popup saying that form isn't valid.
Not sure if this is a bug, or if it's new behaviour that needs to be documented.
You can test this out here
You can enter some text into the array field.
If the value is less than 2 characters, it will add an array to the backlinks' error array. if it's larger, then it'll just return the empty array.
What do you suggest, is this a bug or just a change in behaviour?
What happened instead:
Steps to reproduce:
Related code:
Other information:
Environment
The text was updated successfully, but these errors were encountered: