react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Handle error for values of invalid data types in DateField

Open qbantek opened this issue 1 year ago • 2 comments

This PR proposes changes to handle the scenario when the DateField value does not match the type criteria to be handled as a Date.

Current behavior causes an error when trying to execute date.toLocaleString() on an undefined date value.

DateField.tsx:99 Uncaught TypeError: Cannot read properties of undefined (reading 'toLocaleString')
    at DateFieldImpl (DateField.tsx:99:20)
    at renderWithHooks (react-dom.development.js:16305:18)
    at updateFunctionComponent (react-dom.development.js:19588:20)
    at updateSimpleMemoComponent (react-dom.development.js:19425:10)
    at updateMemoComponent (react-dom.development.js:19284:14)
    at beginWork (react-dom.development.js:21673:16)
    at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16)
    at invokeGuardedCallback (react-dom.development.js:4277:31)
    at beginWork$1 (react-dom.development.js:27451:7)

qbantek avatar Jul 21 '23 23:07 qbantek

@fzaninotto That's a good question indeed. I guess it can't hurt to introduce a new "invalid value" message, which will help both the developer (to see that there is an issue with the data) and the user (to avoid having a runtime error and a blank value).

slax57 avatar Jul 24 '23 14:07 slax57

In all the react-admin fields, only two render a different output in case of error: ReferenceField and ReferenceManyCount.

I think it's a good thing to show to the user that the value is invalid, so I'd recommend we do the same for invalid dates in DateField. In that case, the field could render the same ErrorIcon as ReferenceField.

fzaninotto avatar Jul 27 '23 10:07 fzaninotto