Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuperForms support - typescript issue with constraints #74

Open
Tommertom opened this issue Apr 17, 2023 · 0 comments
Open

SuperForms support - typescript issue with constraints #74

Tommertom opened this issue Apr 17, 2023 · 0 comments

Comments

@Tommertom
Copy link
Owner

Tommertom commented Apr 17, 2023

Superforms works nicely. Just if you want to spread the constraints, the typescript will throw a fit...

And you need to use your own change handler to change the value, as bind:value does not work with webcomponents in Svelte.

(below code is Ionic 6 and does not handle the styling for error/helper in ion-input....so incomplete)

<script lang="ts">
	import { z } from 'zod';

	import { superForm } from 'sveltekit-superforms/client';
	import SuperDebug from 'sveltekit-superforms/client/SuperDebug.svelte';

	const schema = z.object({
		id: z.number().int().positive().default(Date.now()),
		name: z.string().min(2).default('Hello world!'),
		email: z.string().email()
	});

	const data = {
		id: Date.now(),
		name: 'x',
		email: 'sss@gmail.com'
	};

	const { form, errors, message, constraints, enhance, delayed } = superForm(data, {
		SPA: true,
		validators: schema,
		onUpdate: ({ form, cancel }) => {
			console.log('onUpdate', form, cancel);
		},
		onError({ result, message }) {
			console.log('onError', result, message);
			message.set(result.error.message);
		}
	});

	function handleChange(ev) {
		console.log('.');
		$form.name = ev.detail.value;

		// $form.name = 'x' + Date.now();
	}
</script>

<ion-card>
	<ion-card-header>
		<ion-card-subtitle>Great success!!</ion-card-subtitle>
		<ion-card-title>Welcome to your app!</ion-card-title>
	</ion-card-header>

	<ion-card-content>
		Thank you for using this starter. Click buttons below to open these guides (will open in new
		window). Don't forget to open DevTools to see this app in mobile mode. Happy coding!!!

		<SuperDebug data={$form} />

		{#if $message}<h3>{$message}</h3>{/if}
		{$errors.name}
	</ion-card-content>
	<form method="POST" use:enhance>
		<input type="hidden" name="id" bind:value={$form.id} />
		<ion-item>
			<ion-label position="stacked"> Name </ion-label>
			<ion-input
				name="name"
				type="text"
				value={$form.name}
				on:ionInput={handleChange}
				data-invalid={$errors.name} />
			<ion-note slot="helper">
				Here you may enter your first name - or something else
				{#if $errors.name}
					<span class="invalid">{$errors.name}</span>
				{/if}
			</ion-note>
			<ion-note slot="error">Please type more characters...</ion-note>
		</ion-item>
		<ion-item>
			<ion-label position="stacked">
				Email
				<ion-text color="danger">*</ion-text>
			</ion-label>
			<ion-input name="name" type="email" value={$form.email} />
			<ion-note slot="helper">Here you may enter your first name - or something else</ion-note>
			<ion-note slot="error">Please type more characters...</ion-note>
		</ion-item>
		<button>Submit</button>
	</form>
</ion-card>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant