
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
svelte-formify
Advanced tools
A library to manage and validate the forms. This library uses decorators to define validations.
A library to manage and validate the forms. This library uses decorators to define validations.
npm i svelte-formify
This library provides 3 main core components:
@Field
to define properties and validationsSvelteForm
to manage form state, get errors and the validated input values<FormField>
a simple input field to use and prevents boilerplate codes while create input fieldsField decorator expects the declarations from yup
library, you can nest any yup validation function while creating
a Field.
Important: We must pass the Type explicitly for nested types. ESBuild does not support emitDecoratorMetadata yet.
import { number, object, string } from 'yup'
import { Field } from 'svelte-formify'
export class Role {
@Field(string().required().oneOf(['admin', 'test'])) name: string
}
export class User {
@Field(string().required()) username: string
@Field(string().required()) password: string
@Field(string().required()) firstName: string
@Field(string().required()) lastName: string
@Field(object(), Role) role: Role // due to a restriction on ESBuild we can not emit decorator metadata for now, therefore we must pass the type for nested values explicitly
}
SvelteForm class encapsulates the validations, listens for input changes and updates the errors so you can show validates states interactively to the user.
SvelteForm expect 2 parameters while constructing it.
SvelteForm stores all data in a Context
class. A context contains the properties described as below:
You can call getRawValues
function if you need the raw values (e.g. : sending the form)
const values = form.getRawValues()
You can use isValid
property which is a Writable to get validation status each time after user changes something.
Example: you want to enable/disable a button depends on validation status:
<script lang="ts">
import { SvelteForm } from 'svelte-formify'
import { User } from './models/user' // your model classes
const {isValid,...form} = new SvelteForm<User>(User, {
firstName: '',
lastName: '',
password: '',
role: {
name: 'test'
},
username: 'hasan'
})
</script>
<button disabled={!$isValid}>Login</button>
<script lang="ts">
import { SvelteForm } from 'svelte-formify'
import { User } from './models/user' // your model classes
const {values,...form} = new SvelteForm<User>(User, {
firstName: '',
lastName: '',
password: '',
role: {
name: 'test'
},
username: 'hasan'
})
// calling validate manually
form.validate().then(rawValue => {}).catch(validationError => {})
// e.g. usage of listener to get username while user types
$: console.log($values.username.value) // returns the typed value
$: console.log($values.username.error) // returns the error
$: console.log($values.username.touched) // true after onBlur
$: console.log($values.username.dirty) // true while user is typing
</script>
{#if $values.username.error }
<small>show some error</small>
{/if}
<FormField form={form} property={$values.username} classes="w-full p-2" placeholder="Username *" />
<!-- e.g. nested object usage -->
<FormField form={form} property={$values.role.name} classes="w-full p-2" placeholder="Role *" />
FAQs
A library to manage and validate the forms. This library uses decorators to define validations.
We found that svelte-formify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.