Powerful forms in Svelte, inspired by Formik.
Forms in Svelte, inspired by Formik.
Submit
`` is a component for rendering forms. It is largely a port of
Formik to Svelte and broadly
follows the same API. It uses a let:props pattern which is generally similar to
the render prop pattern in React.
{/if}
Submit
enableReinitialize?: booleaninitialErrors?: SveltikErrorsinitialStatus?: anyinitialTouched?: SveltikTouchedinitialValues: ValuesinitialWarnings?: SveltikWarningsonReset?: (values: Values, sveltikBag: SveltikBag) => voidonSubmit: (values: Values, sveltikBag: SveltikBag) => void | Promisevalidate?: (values: Values, sveltikBag: SveltikBag) => SveltikErrorsvalidateOnBlur?: booleanvalidateOnChange?: booleanvalidateOnMount?: booleanlet:props: SveltikPropslet:isDirty: booleanlet:errors: { [field: string]: string }let:handleBlur: (e: HTMLBlurEvent) => voidlet:handleInput: (e: HTMLInputEvent) => voidlet:handleReset: () => voidlet:handleSubmit: (e: HTMLFormEvent) => voidlet:isSubmitting: booleanlet:isValid: booleanlet:isValidating: booleanlet:resetForm: (nextInitialState?: SveltikState) => voidlet:scrollFirstErrorIntoView: () => voidlet:setErrors: (fields: { [field: string]: string }) => voidlet:setFieldError: (field: string, errorMsg: string) => voidlet:setFieldTouched: (field: string, isTouched?: boolean, shouldValidate?: boolean) => voidlet:setFieldValue: (field: string, value: any, shouldValidate?: boolean) => voidlet:setFieldWarning: (field: string, warning: string) => voidlet:setStatus: (status?: any) => voidlet:setSubmitting: (isSubmitting: boolean) => voidlet:setTouched: (fields: { [field: string]: boolean }, shouldValidate?: boolean) => voidlet:setValues: (fields: { [field: string]: any }, shouldValidate?: boolean) => voidlet:setWarnings: (fields: { [field: string]: string }) => voidlet:status: anylet:submitAttemptCount: numberlet:submitFailure: () => voidlet:submitFailureCount: numberlet:submitForm: () => Promiselet:submitSuccess: () => voidlet:submitSuccessCount: numberlet:touched: { [field: string]: boolean }let:values: { [field: string]: any }let:validateForm: (values?: any) => voidlet:validateField: (field: string) => voidlet:warnings: { [field: string]: string }enableReinitialize?: booleanDefault is false. Control whether Sveltik should reset the form if initialValues changes (using deep equality).
initialErrors?: SveltikErrorsInitial field errors of the form, Sveltik will make these values available to render methods component as errors.
initialStatus?: anyAn arbitrary value for the initial status of the form. If the form is reset, this value will be restored.
initialTouched?: SveltikTouchedInitial visitied fields of the form, Sveltik will make these values available to render methods component as touched.
initialValues: ValuesInitial field values of the form, Sveltik will make these values available to render methods component as values.
Even if your form is empty by default, you must initialize all fields with initial values.
onReset?: (values: Values, sveltikBag: SveltikBag) => voidYour optional form reset handler. It is passed your forms values and the "SveltikBag".
onSubmit: (values: Values, sveltikBag: SveltikBag) => void | PromiseYour form submission handler. It is passed your forms values and the "SveltikBag", which includes an object containing a subset of the injected props and methods (i.e. all the methods with names that start with set + resetForm) and any props that were passed to the wrapped component.
If onSubmit returns a promise, rejection automatically calls submitFailure and resolution automatically calls submitSuccess.
validate?: (values: Values) => SveltikErrorsValidate the form's values with function. Synchronous and return an errors object.
validateOnBlur?: booleanDefault is true. Use this option to run validations on blur events. More specifically, when either handleBlur, setFieldTouched, or setTouched are called.
validateOnChange?: booleanDefault is true. Use this option to tell Sveltik to run validations on change events and change-related methods. More specifically, when either handleInput, setFieldValue, or setValues are called.
validateOnMount?: booleanDefault is false. Use this option to tell Sveltik to run validations when the `` component mounts and/or initialValues change.
All of these props are exposed individually as well as a master let:props prop which is an object
of all available props.
let:isDirty: booleanReturns true if values are not deeply equal from initial values, false otherwise. dirty is a readonly computed property and should not be mutated directly.
let:errors: { [field: string]: string }Form validation errors. Should match the shape of your form's values defined in initialValues.
let:handleBlur: (e: HTMLBlurEvent) => voidonBlur event handler. Useful for when you need to track whether an input has been touched or not. This should be passed to
``
let:handleInput: (e: HTMLInputEvent) => voidGeneral input change event handler. This will update the values[key] where key is the event-emitting input's name attribute. If the name attribute is not present, handleInput will look for an input's id attribute. Note: "input" here means all HTML inputs.
let:handleReset: () => voidReset handler. Will reset the form to its initial state. This should be passed to ...
let:handleSubmit: (e: HTMLFormEvent) => voidSubmit handler. This should be passed to ....
let:isSubmitting: booleanSubmitting state of the form. Returns true if submission is in progress and false otherwise.
let:isValid: booleanReturns true if there are no errors (i.e. the errors object is empty) and false otherwise.
let:isValidating: booleanReturns true if Sveltik is running validation during submission, or by calling validateForm directly false otherwise.
let:resetForm: (nextInitialState?: SveltikState) => voidImperatively reset the form. If nextInitialState is specified, Sveltik will set this state as the new "initial state" and use the related values of nextInitialState to update the form's initialValues as well as initialTouched, initialStatus, initialErrors. This is useful for altering the initial state (i.e. "base") of the form after changes have been made. If nextInitialState is not defined, then Sveltik will reset state to the original initial state.
let:scrollFirstErrorIntoView: () => voidScroll the first `` with an error into view. First is calculated by the vertical distance from the top of the document.
let:setErrors: (fields: { [field: string]: string }) => voidSet errors imperatively.
let:setFieldError: (field: string, errorMsg: string) => voidSet the error message of a field imperatively. field should match the key of errors you wish to update. Useful for creating custom input error handlers.
let:setFieldTouched: (field: string, isTouched?: boolean, shouldValidate?: boolean) => voidSet the touched state of a field imperatively. field should match the key of touched you wish to update. Useful for creating custom input blur handlers. Calling this method will trigger validation to run if validateOnBlur is set to true (which it is by default). isTouched defaults to true if not specified. You can also explicitly prevent/skip validation by passing a third argument as false.
let:setFieldValue: (field: string, value: any, shouldValidate?: boolean) => voidSet the value of a field imperatively. field should match the key of values you wish to update. Useful for creating custom input change handlers. Calling this will trigger validation to run if validateOnChange is set to true (which it is by default). You can also explicitly prevent/skip validation by passing a third argument as false.
let:setFieldWarning: (field: string, warning: string) => voidSet the warning of a field imperatively. field should match the key of warnings you wish to update.
let:setStatus: (status?: any) => voidSet a top-level status to anything you want imperatively. Useful for controlling arbitrary top-level state related to your form. For example, you can use it to pass API responses back into your component in handleSubmit.
let:setSubmitting: (isSubmitting: boolean) => voidSet isSubmitting imperatively. You would call it with setSubmitting(false) in your onSubmit handler to finish the cycle.
let:setTouched: (fields: { [field: string]: boolean }, shouldValidate?: boolean) => voidSet touched imperatively. Calling this will trigger validation to run if validateOnBlur is set to true (which it is by default). You can also explicitly prevent/skip validation by passing a second argument as false.
let:setValues: (fields: { [field: string]: any }, shouldValidate?: boolean) => voidSet values imperatively. Calling this will trigger validation to run if validateOnChange is set to true (which it is by default). You can also explicitly prevent/skip validation by passing a second argument as false.
let:setWarnings: (fields: { [field: string]: string }) => voidSet warnings imperatively.
let:status: anyA top-level status object that you can use to represent form state that can't otherwise be expressed/stored with other methods. This is useful for capturing and passing through API responses to your inner component. status should only be modified by calling setStatus.
let:submitAttemptCount: numberNumber of times user tried to submit the form. Increases when handleSubmit is called, resets after calling handleReset.
let:submitFailure: () => voidIncrement submitFailureCount by 1 and calls setSubmitting(false).
let:submitFailureCount: numberNumber of failed form submissions. Increases when submitFailure is called, resets after calling handleReset.
let:submitForm: () => PromiseTrigger a form submission. The promise will be rejected if form is invalid.
let:submitSuccess: () => voidIncrement submitSuccessCount by 1 and calls setSubmitting(false).
let:submitSuccessCount: numberNumber of successful form submissions. Increases when submitSuccess is called, resets after calling handleReset.
let:touched: { [field: string]: boolean }Touched fields. Each key corresponds to a field that has been touched/visited.
let:values: { [field: string]: any }Your form's values.
let:validateForm: (values?: any) => voidImperatively call validate. You can optionally pass values to validate against and this modify Sveltik state accordingly, otherwise this will use the current values of the form.
let:validateField: (field: string) => voidImperatively call field's validate function if specified for given field. Sveltik will use the current field value.
let:warnings: { [field: string]: string }Form warnings. Should match the shape of your form's values defined in initialValues.
No open issues yet, or sync has not completed.