🔒 Biblioteca para formulários com SvelteKit e Zod. 🔒
Svelte Form Zod é uma biblioteca de formulários para SvelteKit que usa Zod para validação de dados.
npm install svelte-form-zod
ou
yarn add svelte-form-zod
Para criar um formulário, você precisa criar um arquivo .svelte
e importar a função createForm
e o z
do Zod.
<script lang="ts">
import { createForm, z } from 'svelte-form-zod';
const schema = z.object({
name: z.string().min(3).max(50),
email: z.string().email(),
password: z.string().min(6).max(50),
phone: z.string(),
cnpj: z.string()
});
const initialValues = {
name: '',
email: '',
password: '',
phone: '',
cnpj: ''
};
const { form, errors } = createForm({
schema,
initialValues,
masked: {
phone: '(99) 9999-9999', // ou ['(99) 9999-9999','(99) 9 9999-9999']
cnpj: '99.999.999/9999-99'
},
onSubmit: (values) => {
const { name, email, password, phone, cnpj } = values;
console.log(name, email, password, phone, cnpj);
}
});
</script>
<form use:form>
<label>
<input type="text" name="name" />
{#if $errors.name}
<span>{$errors.name}</span>
{/if}
</label>
<label>
<input type="text" name="email" />
{#if $errors.email}
<span>{$errors.email}</span>
{/if}
</label>
<label>
<input type="password" name="password" />
{#if $errors.password}
<span>{$errors.password}</span>
{/if}
</label>
<label>
<input type="text" name="phone" />
{#if $errors.phone}
<span>{$errors.phone}</span>
{/if}
</label>
<label>
<input type="text" name="cnpj" />
{#if $errors.cnpj}
<span>{$errors.cnpj}</span>
{/if}
</label>
<button type="submit">Enviar</button>
</form>
createForm
Masked
const { form } = createForm({
schema,
initialValues,
masked: {
phone: '(99) 9999-9999', // ou ['(99) 9999-9999','(99) 9 9999-9999']
uf: 'AA'
},
onSubmit
});
errors
const { form, errors, setError, setErrors, resetError, resetErrors } = createForm({
schema,
initialValues,
onSubmit
})
{#if $errors.name}
<span>{$errors.name}</span>
{/if}
setError('name', 'Nome inválido')
setErrors({
name: 'Nome inválido',
email: 'Email inválido'
})
resetError('name')
resetErrors()
watch
const { form, watch } = createForm({
schema,
initialValues,
onSubmit
})
{#if $watch.name}
<span>{$watch.name}</span>
{/if}
<input type="text" bind:value={$watch.name} />
<input type="text" bind:value={$watch.email} />
updateField
const { form, updateField } = createForm({
schema,
initialValues,
onSubmit
})
updateField('name', 'Name')
updateField('email', 'Email')
ou
<input type="text" on:input={e => updateField('name', e.target.value)} />
<input type="text" on:input={e => updateField('email', e.target.value)} />
updateFields
const { form, updateFields } = createForm({
schema,
initialValues,
onSubmit
})
updateFields({ name: 'Name', email: 'Email' })
ou
<input type="text" on:input={e => updateFields({ name: e.target.value })} />
<input type="text" on:input={e => updateFields({ email: e.target.value })} />
getValue
const { form, getValue } = createForm({
schema,
initialValues,
onSubmit
});
const name = getValue('name');
const email = getValue('email');
getValues
const { form, getValues } = createForm({
schema,
initialValues,
onSubmit
});
const { name, email } = getValues();
reset
const { form, reset } = createForm({
schema,
initialValues,
onSubmit
});
reset();
@viniribeirodev |
---|