Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ragokan/solform

A form management library for SolidJS that is very lightweight and simple!
https://github.com/ragokan/solform

form form-management solidjs

Last synced: 6 days ago
JSON representation

A form management library for SolidJS that is very lightweight and simple!

Awesome Lists containing this project

README

        

# solform

### A form management library for SolidJS that is very lightweight and simple!
#### (UPDATE) Just wanted to say, I was busy a little bit but soon, I will update this package to latest version of everything with all the fixes.
[![npm](https://img.shields.io/npm/v/solform?color=F53B02)](https://www.npmjs.com/package/solform)
[![GitHub Repo stars](https://img.shields.io/github/stars/ragokan/solform?label=github%20stars)](https://github.com/ragokan/solform)

### Why solform

- Very easy, fast, lightweight and powerful!
- It has built-in validation.
- You can easily customize it!
- Full type support

### Create form

```tsx
// Simplest
import { createForm } from "solform";

function App() {
const { register } = createForm<{ name: string }>();

return ;
}
```

```tsx
// Next Level
import { createForm, requiredValidator, emailValidator } from "solform";

function App() {
const { register, submit, errors } = createForm<{
email: string;
count: number;
}>({
validators: {
count: requiredValidator("Count is required"),
email: [
requiredValidator("This field is required"),
emailValidator("Value should be email!"),
],
},
onSubmit: (values) => {
// type safe values
console.log(values);
},
});

return (



{errors.email &&

{errors.email}

}

{/* When you read count, it will be converted to number */}

Submit


);
}
```

```tsx
// Full
import { createForm, requiredValidator, emailValidator } from "solform";

function App() {
const { register, submit, loading, getAllValues, getValue, setValue, errors, watch } =
createForm<{ email: string; count: number }>({
initialValues: {
count: 0,
},
validators: {
count: requiredValidator("Count is required"),
email: [
requiredValidator("This field is required"),
emailValidator("Value should be email!"),
],
},
onSubmit: async (values) => {
await sleep(2000); // it will automatically set loading to true
console.log(values);
// loading is false again
},
});

// will call the given function whenever the count changes
watch("count", (updatedCount) => {
console.log(updatedCount);
});

return (



{errors.email &&

{errors.email}

}
{/* When you read count, it will be converted to number */}


{loading() ? "Loading..." : "Submit"}


);
}

function sleep(arg0: number) {
throw new Error("Function not implemented.");
}
```

### Built-in validators

- minLengthValidator(minLength, errorMessage)
- maxLengthValidator(maxLength, errorMessage)
- requiredValidator(errorMessage)
- emailValidator(errorMessage)

### Creating new validators

```ts
const minimumNumberValidator =
(minNumber: number, errorMessage: string): SolFormValidator =>
(val) =>
val < minNumber ? errorMessage : undefined;
```

### Tactics

- Always use **watch** inside a component, because it will call **onMount** and **onCleanup**.
- More soon...

### What is next?

- Support for more fields, such as **radio group**
- Validate on change option.

### How to contribute?

- Get a fork of this package, do your changes and create a pull request.
- Found a bug? create an issue.