From 915708f2e8395b3552d5d80707435d2e70d3c57f Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Thu, 2 Feb 2023 18:45:34 +0100 Subject: [PATCH] fix: personalId validation --- .../(authenticated)/register/+page.svelte | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte b/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte index 111a235..d02da74 100644 --- a/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte +++ b/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte @@ -49,6 +49,7 @@ let componentErrors = writable( { candidate: { telephone: false, + personalIdMatch: false, }, parents: [ { @@ -59,7 +60,7 @@ } ] }); - let personalIdBirthdateMatch = true; + const formInitialValues = { gdpr: false, personalIdOk: false, @@ -215,32 +216,32 @@ personalIdModal: false, linkErrorModal: false }; + const validatePersonalId = () => { + if ($form.candidate.citizenship === 'Česká republika') { + if ( + !isPersonalIdNumberWithBirthdateValid( + $form.candidate.personalIdNumber, + $form.candidate.birthdate + ) + ) { + toast.push('Rodné číslo neodpovídá oficiální specifikaci či datumu narození', { + theme: { + '--toastColor': 'mintcream', + '--toastBackground': '#b91c1c', + '--toastBarBackground': '#7f1d1d' + } + }); + $componentErrors['candidate']['personalIdMatch'] = true; + throw new Error('Rodné číslo neodpovídá datumu narození'); + } + } + $componentErrors['candidate']['personalIdMatch'] = false; + } const onSubmit = async (values: CandidateData) => { console.log("submit button clicked"); console.log(pagesFilled.map((_, i) => !isPageInvalid(i))); - if (pageIndex === 3) { - if (values.candidate.citizenship === 'Česká republika') { - if ( - !isPersonalIdNumberWithBirthdateValid( - values.candidate.personalIdNumber, - values.candidate.birthdate - ) - ) { - toast.push('Rodné číslo neodpovídá oficiální specifikaci či datumu narození', { - theme: { - '--toastColor': 'mintcream', - '--toastBackground': '#b91c1c', - '--toastBarBackground': '#7f1d1d' - } - }); - personalIdBirthdateMatch = false; - throw new Error('Rodné číslo neodpovídá datumu narození'); - } - } - personalIdBirthdateMatch = true; - } if (pageIndex === pageCount) { console.log('submitting'); // clone values to oldValues @@ -340,7 +341,7 @@ $typedErrors['candidate']['birthplace'] || $typedErrors['candidate']['personalIdNumber'] || $typedErrors['candidate']['testLanguage'] || - !personalIdBirthdateMatch + $componentErrors['candidate']['personalIdMatch'] ) { return true; } @@ -759,11 +760,14 @@
{ + if (pageIndex === 4) { + console.log('validating personal id'); + validatePersonalId(); + } await handleSubmit(e); console.log(pagesFilled.map((_, i) => !isPageInvalid(i))); if (isPageInvalid(pageIndex)) return; - if (pageIndex === pageCount) { - } else { + if (pageIndex !== pageCount) { pagesFilled[pageIndex] = true; pageIndex++; } @@ -779,6 +783,9 @@