fix: personalId validation

This commit is contained in:
Sebastian Pravda 2023-02-02 18:45:34 +01:00 committed by EETagent
parent 911a5f6ee2
commit 915708f2e8

View file

@ -49,6 +49,7 @@
let componentErrors = writable( { let componentErrors = writable( {
candidate: { candidate: {
telephone: false, telephone: false,
personalIdMatch: false,
}, },
parents: [ parents: [
{ {
@ -59,7 +60,7 @@
} }
] ]
}); });
let personalIdBirthdateMatch = true;
const formInitialValues = { const formInitialValues = {
gdpr: false, gdpr: false,
personalIdOk: false, personalIdOk: false,
@ -215,17 +216,12 @@
personalIdModal: false, personalIdModal: false,
linkErrorModal: false linkErrorModal: false
}; };
const validatePersonalId = () => {
const onSubmit = async (values: CandidateData) => { if ($form.candidate.citizenship === 'Česká republika') {
console.log("submit button clicked");
console.log(pagesFilled.map((_, i) => !isPageInvalid(i)));
if (pageIndex === 3) {
if (values.candidate.citizenship === 'Česká republika') {
if ( if (
!isPersonalIdNumberWithBirthdateValid( !isPersonalIdNumberWithBirthdateValid(
values.candidate.personalIdNumber, $form.candidate.personalIdNumber,
values.candidate.birthdate $form.candidate.birthdate
) )
) { ) {
toast.push('Rodné číslo neodpovídá oficiální specifikaci či datumu narození', { toast.push('Rodné číslo neodpovídá oficiální specifikaci či datumu narození', {
@ -235,12 +231,17 @@
'--toastBarBackground': '#7f1d1d' '--toastBarBackground': '#7f1d1d'
} }
}); });
personalIdBirthdateMatch = false; $componentErrors['candidate']['personalIdMatch'] = true;
throw new Error('Rodné číslo neodpovídá datumu narození'); throw new Error('Rodné číslo neodpovídá datumu narození');
} }
} }
personalIdBirthdateMatch = true; $componentErrors['candidate']['personalIdMatch'] = false;
} }
const onSubmit = async (values: CandidateData) => {
console.log("submit button clicked");
console.log(pagesFilled.map((_, i) => !isPageInvalid(i)));
if (pageIndex === pageCount) { if (pageIndex === pageCount) {
console.log('submitting'); console.log('submitting');
// clone values to oldValues // clone values to oldValues
@ -340,7 +341,7 @@
$typedErrors['candidate']['birthplace'] || $typedErrors['candidate']['birthplace'] ||
$typedErrors['candidate']['personalIdNumber'] || $typedErrors['candidate']['personalIdNumber'] ||
$typedErrors['candidate']['testLanguage'] || $typedErrors['candidate']['testLanguage'] ||
!personalIdBirthdateMatch $componentErrors['candidate']['personalIdMatch']
) { ) {
return true; return true;
} }
@ -759,11 +760,14 @@
<div class="field"> <div class="field">
<Submit <Submit
on:click={async (e) => { on:click={async (e) => {
if (pageIndex === 4) {
console.log('validating personal id');
validatePersonalId();
}
await handleSubmit(e); await handleSubmit(e);
console.log(pagesFilled.map((_, i) => !isPageInvalid(i))); console.log(pagesFilled.map((_, i) => !isPageInvalid(i)));
if (isPageInvalid(pageIndex)) return; if (isPageInvalid(pageIndex)) return;
if (pageIndex === pageCount) { if (pageIndex !== pageCount) {
} else {
pagesFilled[pageIndex] = true; pagesFilled[pageIndex] = true;
pageIndex++; pageIndex++;
} }
@ -779,6 +783,9 @@
<button <button
class:dotActive={i === pageIndex} class:dotActive={i === pageIndex}
on:click={async (e) => { on:click={async (e) => {
if (pageIndex === 4 && i > pageIndex) {
validatePersonalId();
}
pageIndex -= pageIndex === pageCount ? 1 : 0; pageIndex -= pageIndex === pageCount ? 1 : 0;
await handleSubmit(e); await handleSubmit(e);
pagesFilled = pagesFilled.map((_, i) => !isPageInvalid(i)); pagesFilled = pagesFilled.map((_, i) => !isPageInvalid(i));