From e25dd7b3d35e6a0d1e31f00daac80bc6adec6fbf Mon Sep 17 00:00:00 2001 From: Bryce Willey Date: Fri, 19 Jan 2024 00:11:43 -0500 Subject: [PATCH 1/5] Check isNaN on alMax & use today as birthdate max Finish the code done in https://github.com/SuffolkLITLab/docassemble-ALToolbox/pull/245. --- docassemble/ALToolbox/ThreePartsDate.py | 30 +++++++++++++------------ 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/docassemble/ALToolbox/ThreePartsDate.py b/docassemble/ALToolbox/ThreePartsDate.py index 2e16833..bdeef25 100644 --- a/docassemble/ALToolbox/ThreePartsDate.py +++ b/docassemble/ALToolbox/ThreePartsDate.py @@ -510,14 +510,14 @@ // Non-MVP. Make an issue. // Replace '-' in case it's an ISO date format, (our recommended format). // JS doesn't play nicely with ISO format. - var min_attr = get_$original_date(field).attr('data-alMin'); - if (!min_attr) {{ - // Validation should always succeed if no minimum given + let min_attr = get_$original_date(field).attr('data-alMin') || ""; + let min_date = new Date(min_attr.replace(/-/g, '/')); + if (isNaN(min_attr)) {{ + // Validation should always succeed if no or bad minimum given return true; }} - var min_date = new Date(min_attr.replace(/-/g, '/')); - // Avoid usinig `params`, which could be in many different formats. + // Avoid using `params`, which could be in many different formats. // Just get date data from the actual fields var data = get_date_data(field); var date_input = new Date(data.year + '/' + data.month + '/' + data.day); @@ -543,17 +543,19 @@ }} // TODO: Catch invalid alMax attr values for devs? Log in console? Make post MVP issue - let max_attr = get_$original_date(field).attr('data-alMax'); - if (!max_attr) {{ - // Validation should always succeed if no minimum given - return true; - }} - let max_date = new Date(max_attr.replace(/-/g, '/')); - if ( isNaN(max_date) && is_birthdate(field)) {{ - max_date = new Date(); + let max_attr = get_$original_date(field).attr('data-alMax') || ""; + let max_date = new Date(max_attr); + if (isNaN(max_attr)) {{ + if (!is_birthdate(field)) {{ + // Validation should always succeed if no or bad max given on normal dates + return true; + }} else {{ + // if birthdate and no or bad max given, assume today is the max date + max_date = new Date(); + }} }} - // Avoid usinig `params`, which could be in many different formats. + // Avoid using `params`, which could be in many different formats. // Just get date data from the actual fields var data = get_date_data(field); var date_input = new Date(data.year + '/' + data.month + '/' + data.day); From 544caca2d322645eab819fbd4251bb0901f69497 Mon Sep 17 00:00:00 2001 From: Bryce Willey Date: Fri, 19 Jan 2024 00:17:56 -0500 Subject: [PATCH 2/5] Log when an invalid date is seen --- docassemble/ALToolbox/ThreePartsDate.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docassemble/ALToolbox/ThreePartsDate.py b/docassemble/ALToolbox/ThreePartsDate.py index bdeef25..f5b51b9 100644 --- a/docassemble/ALToolbox/ThreePartsDate.py +++ b/docassemble/ALToolbox/ThreePartsDate.py @@ -513,6 +513,9 @@ let min_attr = get_$original_date(field).attr('data-alMin') || ""; let min_date = new Date(min_attr.replace(/-/g, '/')); if (isNaN(min_attr)) {{ + if (min_attr !== "") { + console.log(`The alMin attribute (${ min_attr }) isn't a valid date!`); + } // Validation should always succeed if no or bad minimum given return true; }} @@ -546,6 +549,9 @@ let max_attr = get_$original_date(field).attr('data-alMax') || ""; let max_date = new Date(max_attr); if (isNaN(max_attr)) {{ + if (max_attr !== "") { + console.log(`The alMax attribute (${ max_attr }) isn't a valid date!`); + } if (!is_birthdate(field)) {{ // Validation should always succeed if no or bad max given on normal dates return true; From 8c94457fe5481f0457d631aee687d268996113fa Mon Sep 17 00:00:00 2001 From: Bryce Willey Date: Mon, 22 Jan 2024 22:42:07 -0500 Subject: [PATCH 3/5] Add missing parens in JS code string Co-authored-by: plocket <52798256+plocket@users.noreply.github.com> --- docassemble/ALToolbox/ThreePartsDate.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docassemble/ALToolbox/ThreePartsDate.py b/docassemble/ALToolbox/ThreePartsDate.py index f5b51b9..b072bda 100644 --- a/docassemble/ALToolbox/ThreePartsDate.py +++ b/docassemble/ALToolbox/ThreePartsDate.py @@ -513,9 +513,9 @@ let min_attr = get_$original_date(field).attr('data-alMin') || ""; let min_date = new Date(min_attr.replace(/-/g, '/')); if (isNaN(min_attr)) {{ - if (min_attr !== "") { - console.log(`The alMin attribute (${ min_attr }) isn't a valid date!`); - } + if (min_attr !== "") {{ + console.log(`The alMin attribute (${{ min_attr }}) isn't a valid date!`); + }} // Validation should always succeed if no or bad minimum given return true; }} @@ -549,9 +549,9 @@ let max_attr = get_$original_date(field).attr('data-alMax') || ""; let max_date = new Date(max_attr); if (isNaN(max_attr)) {{ - if (max_attr !== "") { - console.log(`The alMax attribute (${ max_attr }) isn't a valid date!`); - } + if (max_attr !== "") {{ + console.log(`The alMax attribute (${{ max_attr }}) isn't a valid date!`); + }} if (!is_birthdate(field)) {{ // Validation should always succeed if no or bad max given on normal dates return true; From 9826c949e633b31353229d27162cdf215132913a Mon Sep 17 00:00:00 2001 From: Bryce Willey Date: Tue, 23 Jan 2024 22:35:38 -0500 Subject: [PATCH 4/5] Testing changes: * refer to correct date var in `isNaN` * do similar logic in message functions to prevent errors * default to showing "birthdate must be in past" when birthdate max is invalid --- docassemble/ALToolbox/ThreePartsDate.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docassemble/ALToolbox/ThreePartsDate.py b/docassemble/ALToolbox/ThreePartsDate.py index b072bda..e5a2d69 100644 --- a/docassemble/ALToolbox/ThreePartsDate.py +++ b/docassemble/ALToolbox/ThreePartsDate.py @@ -512,7 +512,7 @@ // JS doesn't play nicely with ISO format. let min_attr = get_$original_date(field).attr('data-alMin') || ""; let min_date = new Date(min_attr.replace(/-/g, '/')); - if (isNaN(min_attr)) {{ + if (isNaN(min_date)) {{ if (min_attr !== "") {{ console.log(`The alMin attribute (${{ min_attr }}) isn't a valid date!`); }} @@ -529,7 +529,8 @@ }}, function alMinMessage (validity, field) {{ /** Returns the string of the invalidation message. */ - let min_date = new Date(get_$original_date(field).attr('data-alMin').replace(/-/g, '/')); + let min_attr = get_$original_date(field).attr('data-alMin') || ""; + let min_date = new Date(min_attr.replace(/-/g, '/')); let locale_long_date = min_date.toLocaleDateString(undefined, {{ day: '2-digit', month: 'long', year: 'numeric' }}); return ( get_$original_date(field).attr('data-alMinMessage') @@ -547,8 +548,8 @@ // TODO: Catch invalid alMax attr values for devs? Log in console? Make post MVP issue let max_attr = get_$original_date(field).attr('data-alMax') || ""; - let max_date = new Date(max_attr); - if (isNaN(max_attr)) {{ + let max_date = new Date(max_attr.replace(/-/g, '/')); + if (isNaN(max_date)) {{ if (max_attr !== "") {{ console.log(`The alMax attribute (${{ max_attr }}) isn't a valid date!`); }} @@ -570,11 +571,12 @@ }}, function alMaxMessage (validity, field) {{ /** Returns the string of the invalidation message. */ - let max_date = new Date(get_$original_date(field).attr('data-alMax').replace(/-/g, '/')); + let max_attr = get_$original_date(field).attr('data-alMax') || ""; + let max_date = new Date(max_attr.replace(/-/g, '/')); let locale_long_datetime = max_date.toLocaleDateString(undefined, {{ day: '2-digit', month: 'long', year: 'numeric' }}) let default_MaxMessage = `The date needs to be on or before ${{ locale_long_datetime }}.`; // Birthdays have a different default max message - if (!get_$original_date(field).attr('data-alMax') && is_birthdate(field)) {{ + if (is_birthdate(field) && isNaN(max_date)) {{ default_MaxMessage = 'A birthdate must be in the past.'; }} From 306bd9009e1067dbcf3e14476af8ad01602a932e Mon Sep 17 00:00:00 2001 From: Bryce Willey Date: Mon, 5 Feb 2024 21:27:47 -0500 Subject: [PATCH 5/5] Comment pointing out consistency --- docassemble/ALToolbox/ThreePartsDate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docassemble/ALToolbox/ThreePartsDate.py b/docassemble/ALToolbox/ThreePartsDate.py index e5a2d69..c4a256d 100644 --- a/docassemble/ALToolbox/ThreePartsDate.py +++ b/docassemble/ALToolbox/ThreePartsDate.py @@ -528,7 +528,8 @@ return date_input >= min_date; }}, function alMinMessage (validity, field) {{ - /** Returns the string of the invalidation message. */ + /** Returns the string of the invalidation message, or blank string for + * safety and consistency with alMaxMessage. */ let min_attr = get_$original_date(field).attr('data-alMin') || ""; let min_date = new Date(min_attr.replace(/-/g, '/')); let locale_long_date = min_date.toLocaleDateString(undefined, {{ day: '2-digit', month: 'long', year: 'numeric' }});