Skip to content

Commit c5f4ede

Browse files
committed
addError method
1 parent 8243c24 commit c5f4ede

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# svelte-command-form
22

3-
Modern form state management for Svelte built on top of the [Standard Schema](https://github.com/standard-schema/standard-schema) protocol. `svelte-command-form` helps you pair type-safe validators with imperative “command” functions (load actions, server functions, RPC endpoints, etc.) while keeping client state, server errors, and validation feedback perfectly aligned.
3+
Svelte-Command-Form allows you to have easy to use forms with commands instead of remote forms. Is this redundant? Maybe. However, you may not want to use an HTML form everytime. The API is greatly influenced by SvelteKit-Superforms, so if you are used to that you shouldn't have a problem here.
44

55
## Features
66

@@ -13,9 +13,9 @@ Modern form state management for Svelte built on top of the [Standard Schema](ht
1313
## Installation
1414

1515
```bash
16-
pnpm add svelte-command-form
16+
pnpm add @akcodeworks/svelte-command-form
1717
# or
18-
npm install svelte-command-form
18+
npm install @akcodeworks/svelte-command-form
1919
```
2020

2121
## Quick start
@@ -39,7 +39,7 @@ npm install svelte-command-form
3939
<p class="error">{form.errors.name.message}</p>
4040
{/if}
4141

42-
<button disabled="{form.submitting}" on:click|preventDefault="{form.submit}">
42+
<button disabled="{form.submitting}" onclick="{form.submit}">
4343
{form.submitting ? 'Saving…' : 'Save'}
4444
</button>
4545
```
@@ -127,6 +127,7 @@ const form = new CommandForm(userSchema, { command: saveUser });
127127
- `validate()` – Runs schema validation without submitting, updating both `errors` and `issues`.
128128
- `submit()` – Parses the schema, calls hooks, executes the configured command, manages invalidation, and populates error state on failure.
129129
- `getErrors()` / `getIssues()` – Accessor helpers useful outside of `$state` reactivity (e.g., from tests).
130+
- `addError({path: string, message: string})` - Allows you to set an error on the form programatically (client side only)
130131

131132
### `standardValidate(schema, input)`
132133

@@ -163,7 +164,7 @@ SvelteKit command functions currently expect JSON-serializable payloads, so `Fil
163164
};
164165
</script>
165166

166-
<input type="file" multiple on:change="{handleFiles}" />
167+
<input type="file" multiple onchange="{handleFiles}" />
167168
```
168169

169170
`normalizeFiles` outputs objects like:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@akcodeworks/svelte-command-form",
3-
"version": "0.0.1",
3+
"version": "0.0.0",
44
"description": "Command pattern form helper for Svelte powered by Standard Schema validation.",
55
"scripts": {
66
"dev": "vite dev",

src/lib/command-form/command-form.svelte.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export class CommandForm<Schema extends StandardSchemaV1, TOut> {
5858
return standardValidate(this.schema, this.form as StandardSchemaV1.InferInput<Schema>);
5959
}
6060

61+
// set path to keyof schema or string
62+
63+
addError = (error: { path: string, message: string }) => {
64+
(this.errors as any)[error.path] = { message: error.message };
65+
}
66+
6167
reset = () => {
6268
for (const key in this.form) delete (this.form as any)[key];
6369
Object.assign(this.form, this.resolveInitial());

src/routes/+page.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
</script>
2424

2525
<input type="text" placeholder="Name" bind:value={f.form.name} />
26+
2627
<button onclick={f.submit}>submit</button>
28+
<button onclick={() => f.addError({ path: 'name', message: 'yeet' })}>custom error</button>
2729
{#if f.errors.name?.message}
2830
<p style="color: red">{f.errors.name.message}</p>
2931
{/if}

0 commit comments

Comments
 (0)