-
-
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
Handle error for values of invalid data types in DateField #9119
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -61,7 +61,18 @@ const DateFieldImpl = < | |||
} | |||
|
|||
const value = get(record, source); | |||
if (value == null || value === '') { | |||
const parsedDate = (value: any) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doin't understand why you extracted to a function just to call it afterwards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it to encapsulate the logic for validating whether the value can represent a Date or not. Probably a matter of style and personal preference, I can change it to inline code obviously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, please inline this code
}; | ||
const date = parsedDate(value); | ||
|
||
if (typeof date === 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the value == null
test instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I understood this request properly: Are you asking to have BOTH value == null
and typeof date === 'undefined'
or to get rid if the undefined
criteria?
The way I see it the null check is handled before (in parsedDate
function) and the undefined check is needed to avoid the error, kinda the whole point of this PR. Please correct me if I am wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No you're right, the check is already made in parsedDate, you can keep your code as is for this part.
@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). |
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. |
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.