felte icon indicating copy to clipboard operation
felte copied to clipboard

[question] [Sveltekit] How to validate on submit?

Open m0nm opened this issue 1 year ago • 4 comments

Hello, Great package btw,

The behavior I'm trying to achieve is to show the validation errors after the user submit the form, the default behavior is validating on keydown as soon as the input is touched.

Here is the solution that i tried:

<script lang="ts">
	import { createForm, getValue } from 'felte';
	import { validator } from '@felte/validator-zod';
	import { schema} from '$lib/validation/sign-up-schema';

	const { form, errors, unsetField, validate } = createForm({
		extend: [validator({ schema })],
		onSubmit: (v, { form }) => {
                        validate()
			form?.submit();
		}
	});

        unsetField("email")
	unsetField("password")
	unsetField("passwordConfirm")
</script>

{#each Object.entries($errors) as [label, message]}
		{#if message}
			<div class="">
				<div class="">
					<h5 class="">Error: {label} is invalid</h5>
					<p>{message}</p>
				</div>
			</div>
		{/if}
{/each}

<form use:form>
        /.../
           </form>

I hope this is not a duplicate, Thanks in advance.

m0nm avatar Jul 26 '23 00:07 m0nm

@pablo-abc This feature is important to prevent this behavior:

https://github.com/pablo-abc/felte/assets/6722075/c19e22f7-f5d5-4187-9b71-cf5a0c6aa687

stefanosandes avatar Nov 18 '23 13:11 stefanosandes

This would be very useful. Is there a way to implement it? @m0nm @stefanosandes have you found the solution?

bartoszkrawczyk2 avatar Nov 22 '23 23:11 bartoszkrawczyk2

Hello, useForm has the mode prop which does this, maybe it can be added here to.

https://react-hook-form.com/docs/useform#mode

andelkocvjetkovic avatar Jan 17 '24 09:01 andelkocvjetkovic

Love it, a big plus from myself: I think this is related: https://github.com/pablo-abc/felte/discussions/129 https://github.com/pablo-abc/felte/discussions/129 https://github.com/pablo-abc/felte/issues/289

ivanafanasyeu avatar Apr 24 '24 12:04 ivanafanasyeu