mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-23 00:21:47 +00:00
feat: validate phone number in yup directly
This commit is contained in:
parent
4ec8f3596d
commit
699f442088
2 changed files with 14 additions and 10 deletions
|
|
@ -20,12 +20,6 @@
|
|||
export let error: string = '';
|
||||
$: error = valid ? '' : 'Zadejte platný telefon s předvolbou. Například +420 123 456 789';
|
||||
|
||||
$: {
|
||||
if (!error) {
|
||||
error = valid ? '' : 'Zadejte platný telefon s předvolbou. Například +420 123 456 789';
|
||||
}
|
||||
}
|
||||
|
||||
// Optional - Extended details about the parsed phone number
|
||||
let parsedTelInput: NormalizedTelNumber | null = null;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
import { apiFillDetails } from '$lib/@api/candidate';
|
||||
import Submit from '$lib/components/button/Submit.svelte';
|
||||
import GdprCheckBox from '$lib/components/checkbox/GdprCheckBox.svelte';
|
||||
|
||||
import SchoolBadge from '$lib/components/icons/SchoolBadge.svelte';
|
||||
import SplitLayout from '$lib/components/layout/SplitLayout.svelte';
|
||||
import SelectField from '$lib/components/select/SelectField.svelte';
|
||||
|
|
@ -16,7 +15,7 @@
|
|||
import TextField from '$lib/components/textfield/TextField.svelte';
|
||||
import type { PageData } from './$types';
|
||||
import { SvelteToast, toast } from '@zerodevx/svelte-toast';
|
||||
|
||||
import parsePhoneNumber from 'libphonenumber-js';
|
||||
import { createForm } from 'svelte-forms-lib';
|
||||
import * as yup from 'yup';
|
||||
import type { CandidateData } from '$lib/stores/candidate';
|
||||
|
|
@ -104,7 +103,15 @@
|
|||
name: yup.string().required(),
|
||||
surname: yup.string().required(),
|
||||
email: yup.string().email().required(),
|
||||
telephone: yup.string().required(), // already validated by the 'TelephoneField' component
|
||||
telephone: yup
|
||||
.string()
|
||||
.required()
|
||||
.test((_val) => {
|
||||
if (!_val) return false;
|
||||
const number = parsePhoneNumber(_val);
|
||||
if (!number) return false;
|
||||
return number.isValid();
|
||||
}), // already validated by the 'TelephoneField' component
|
||||
birthplace: yup.string().required(),
|
||||
birthdate: yup
|
||||
.string()
|
||||
|
|
@ -175,7 +182,10 @@
|
|||
if (context.path.includes('parents[1]')) {
|
||||
return true;
|
||||
}
|
||||
return _val !== '';
|
||||
if (!_val) return false;
|
||||
const number = parsePhoneNumber(_val);
|
||||
if (!number) return false;
|
||||
return number.isValid();
|
||||
})
|
||||
})
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue