Skip to content
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

Prevent invalid date written into xml #7321

Merged
merged 3 commits into from
Sep 22, 2023

Conversation

wangf1122
Copy link
Collaborator

@wangf1122 wangf1122 commented Sep 6, 2023

This issue happens in both GN4 and lower versions (3.12).

If the user select datetime for the field, pick a time but not a date.
image

Switch to xml view first time, the field will just render empty string which has no ill effect on the Java side because Java ISO date will just use default date 1970-01-01
image

switch between xml view and UI view second time (or validate the records couple of times).
The "Invalid Date" string was put to the xml.
image

This can cause the xml parser on Java side returns some exception.

This "Invalid Date" is some error message when trying to initiate a Date object in Javascript and this can be ignored

@wangf1122 wangf1122 marked this pull request as ready for review September 6, 2023 13:56
@josegar74
Copy link
Member

@wangf1122 it works, but maybe a better solution to avoid checking the text of the error could be to update the following code:

scope.date = new Date(
moment(isDateTime ? tokens[0] : scope.value)
.utc()
.format()
);

Checking moment(isDateTime ? tokens[0] : scope.value).isValid(), if it is not valid assign scope.date = '' instead of the previous assignment, that should not require further changes.

@wangf1122
Copy link
Collaborator Author

@josegar74

Yeah, I think the code you showed is the better place. It is where the variable was initiated. I tested and it works good. I did make the code update to this comment

@josegar74
Copy link
Member

@wangf1122 my proposal was a bit different, to avoid checking the text value Invalid date:

if (moment(isDateTime ? tokens[0] : scope.value).isValid()) {
 scope.date = new Date( 
   moment(isDateTime ? tokens[0] : scope.value) 
     .utc() 
     .format() 
 ); 
} else {
 scope.date = '';
}

@wangf1122
Copy link
Collaborator Author

@josegar74

I have tested your code and works good. I made another commit for it.

@josegar74 josegar74 merged commit 50cfa4c into geonetwork:main Sep 22, 2023
6 checks passed
josegar74 pushed a commit that referenced this pull request Sep 22, 2023
* Prevent invalid date written into xml

* set invalid date prevention at the variable initiating place

* default date and check isValid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants