Skip to content

Commit

Permalink
ilmo form validate everything
Browse files Browse the repository at this point in the history
  • Loading branch information
allurei committed Dec 30, 2024
1 parent 3221d5c commit b9603e5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/lib/components/ilmoformi.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<div class="flex-item six">
<label>
<span>{fi ? 'Opintojen aloitusvuosi' : 'Starting year of studies'}</span>
<input type="number" name="starting_year">
<input type="number" name="starting_year" min="1950" max="2025">
</label>
</div>
<div class="flex-item six">
Expand Down Expand Up @@ -105,7 +105,7 @@
</div>
<div class="flex-item six">
<label>
<span>{fi ? 'Avecin nimi (HUOM! Avecing on ilmoittauduttava erikseen)' : '(NOTE! Avecs need to register separately)'}</span>
<span>{fi ? 'Avecin nimi (HUOM! Avecing on ilmoittauduttava erikseen)' : 'Avecs name (NOTE! Avecs need to register separately)'}</span>
<input type="text" name="avecs_name">
</label>
</div>
Expand Down Expand Up @@ -167,6 +167,11 @@
?'Ilmoittautuminen ei ole auki!'
: 'Registration is not open!'}
</p>
{:else if form?.fields === true}
<p class="bold">{fi
?'Täytä pakolliset kentät (*)'
: 'Fill mandatory fields (*)'}
</p>
{:else}
<p class="bold">{fi
?'Jotain meni pieleen. Yritä hetken kuluttua uudelleen.'
Expand Down
44 changes: 42 additions & 2 deletions src/routes/tesmi/en/ilmo/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,47 @@ export const actions = {
const data = await request.formData();

let body = {};
data.forEach((value, key) => body[key] = value);
let error = false;

if (!data.has('is_consenting') || !data.has('is_asteriski_member') || !data.has('is_alcohol_free') || !data.has('menu') || !data.has('is_attending_sillis') || !data.has('show_name')) {
return fail(400, {
success: false,
fields: true
})
}

if (data.get('is_greeting') === 'yes' && data.get('party_representing') === '') {
return fail(400, {
success: false,
fields: true
})
}

const starting_year = parseInt(data.get('starting_year'));
if (starting_year < 1950 || starting_year > 2025) {
return fail(400, {
success: false,
fields: true
})
}

data.forEach((value, key) => {
if (['first_name', 'last_name', 'email'].includes(key) && value === '') {
error = true
}
if (key === 'starting_year' && value === '') {
return;
}
body[key] = value;
});

if (error) {
return fail(400, {
success: false,
fields: true
})
}

body = JSON.stringify(body);

const response = await fetch('https://api.viiskyt.asteriski.fi/api/participants/', {
Expand All @@ -21,7 +61,7 @@ export const actions = {
success: true,
};
}
if (res?.non_field_errors[0] === 'Ilmoittautuminen ei ole auki') {
if (res?.non_field_errors && Array.isArray(res.non_field_errors) && res?.non_field_errors[0] === 'Ilmoittautuminen ei ole auki') {
return fail(400, {
success: false,
ilmo: false
Expand Down
46 changes: 43 additions & 3 deletions src/routes/tesmi/ilmo/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
import { fail } from '@sveltejs/kit';

export const actions = {
invited: async ({ request }) => {
default: async ({ request }) => {
const data = await request.formData();

let body = {};
data.forEach((value, key) => body[key] = value);
let error = false;

if (!data.has('is_consenting') || !data.has('is_asteriski_member') || !data.has('is_alcohol_free') || !data.has('menu') || !data.has('is_attending_sillis') || !data.has('show_name')) {
return fail(400, {
success: false,
fields: true
})
}

if (data.get('is_greeting') === 'yes' && data.get('party_representing') === '') {
return fail(400, {
success: false,
fields: true
})
}

const starting_year = parseInt(data.get('starting_year'));
if (starting_year < 1950 || starting_year > 2025) {
return fail(400, {
success: false,
fields: true
})
}

data.forEach((value, key) => {
if (['first_name', 'last_name', 'email'].includes(key) && value === '') {
error = true
}
if (key === 'starting_year' && value === '') {
return;
}
body[key] = value;
});

if (error) {
return fail(400, {
success: false,
fields: true
})
}

body = JSON.stringify(body);

const response = await fetch('https://api.viiskyt.asteriski.fi/api/participants/', {
Expand All @@ -21,7 +61,7 @@ export const actions = {
success: true,
};
}
if (res?.non_field_errors[0] === 'Ilmoittautuminen ei ole auki') {
if (res?.non_field_errors && Array.isArray(res.non_field_errors) && res?.non_field_errors[0] === 'Ilmoittautuminen ei ole auki') {
return fail(400, {
success: false,
ilmo: false
Expand Down
44 changes: 42 additions & 2 deletions src/routes/tesmi/kutsuvieras-ilmo/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,47 @@ export const actions = {
const data = await request.formData();

let body = {};
data.forEach((value, key) => body[key] = value);
let error = false;

if (!data.has('is_consenting') || !data.has('is_asteriski_member') || !data.has('is_alcohol_free') || !data.has('menu') || !data.has('is_attending_sillis') || !data.has('show_name')) {
return fail(400, {
success: false,
fields: true
})
}

if (data.get('is_greeting') === 'yes' && data.get('party_representing') === '') {
return fail(400, {
success: false,
fields: true
})
}

const starting_year = parseInt(data.get('starting_year'));
if (starting_year < 1950 || starting_year > 2025) {
return fail(400, {
success: false,
fields: true
})
}

data.forEach((value, key) => {
if (['first_name', 'last_name', 'email'].includes(key) && value === '') {
error = true
}
if (key === 'starting_year' && value === '') {
return;
}
body[key] = value;
});

if (error) {
return fail(400, {
success: false,
fields: true
})
}

body = JSON.stringify(body);

const response = await fetch('https://api.viiskyt.asteriski.fi/api/participants/', {
Expand All @@ -21,7 +61,7 @@ export const actions = {
success: true,
};
}
if (res?.non_field_errors[0] === 'Ilmoittautuminen ei ole auki') {
if (res?.non_field_errors && Array.isArray(res.non_field_errors) && res?.non_field_errors[0] === 'Ilmoittautuminen ei ole auki') {
return fail(400, {
success: false,
ilmo: false
Expand Down

0 comments on commit b9603e5

Please sign in to comment.