# reformer

> Super customizable, Self-contained, self-rendering, self-validating forms that can only output valid data.

Latest version **1.4.2** (published 2013-07-23) · 0 weekly downloads

## Install

```sh
npm install reformer
pnpm add reformer
yarn add reformer
bun add reformer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.2 |
| Published | 2013-07-23 |
| First published | 2012-09-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Henrik Joreteg |
| Maintainers | henrikjoreteg |
| Keywords | form, browser |

## Links

- npm: https://www.npmjs.com/package/reformer
- Repository: https://github.com/HenrikJoreteg/reformer
- npm.io page: https://npm.io/package/reformer

## Dependencies (1)

- [domify](https://npm.io/package/domify.md) 1.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.4.2 (latest) — 2013-07-23
- 1.4.1 — 2013-07-23
- 1.4.0 — 2013-07-23
- 1.3.0 — 2013-07-23
- 1.2.0 — 2013-07-22
- 1.1.2 — 2013-07-16
- 1.1.1 — 2013-07-16
- 1.1.0 — 2013-07-12
- 1.0.1 — 2013-07-12
- 1.0.0 — 2013-07-12
- 0.0.5 — 2013-03-27
- 0.0.4 — 2013-01-24
- 0.0.3 — 2013-01-24
- 0.0.2 — 2013-01-14
- 0.0.1 — 2012-09-14

## README

#reformer

Self-contained, self-rendering, self-validating forms that can only output valid data.

##Step #1: Define your fields, with as many tests as you want for each field:

```javascript
var f = new Reformer({
  fields: [
    {
      name: 'first_name',
      label: 'First Name',
      placeholder: 'Something',
      required: true
    },
    {
      name: 'last_name',
      label: 'Last Name',
      tests: [
        {
          test: function (val) {
            return false;
          },
          message: 'something will always go wrong'
        },
        {
          test: function (val) {
            return val && val.toString().length > 2;
          },
          message: 'Must be at least three characters.'
        }
      ],
      required: true
    }
  ],
  submit: function (vals) {
    console.log(vals);
  },
  error: function (vals) {
    console.log('error', vals);
  }
});
```


##Step #2: Render it once. It handles itself after that:
   
```javascript 
document.addEventListener('DOMContentLoaded', function () {
  // render our form from about, just tell it where.
  f.render({
    formEl: document.getElementById('myform'),
    fieldContainer: document.getElementById('fieldContainer'),
  });
});
```

Reformer will handle form submit, and call your callback if everything's happy.

##Bonus Step #3: Asynchronous validation

You know how you've always got that one field that needs to be checked via ajax. It's a pain. Because most of your tests are simple regexes that can be run synchronously except for this one stupid ajax call to check if a username is available. So, just do this:

```javascript
// your field definition
{
  name: 'email',
  label: 'Email',
  tests: [
    // BAM! add the `async` flag and your test will receive a third argument. A callback.
    {
      async: true, 
      test: function (email, formInstance, cb) {
        $.get('/email-is-avail?val=' + email, function (data) {
          cb(data === '1');
        });
      },
      message: 'This email is already in use.'
    },
    // You can have simple synchronous tests alongside as well.
    // and it still Just Works™
    {
      test: _.isEmail,
      message: 'This doesn\'t seem like a real email address'
    }
  ],
  required: true
}
```

It's simple, but that's all for now. Because it works for what I need for this particular app. Contributions welcomed.

Cheers,

 \- [@HenrikJoreteg](http://twitter.com/henrikjoreteg)

##Gotchas

 - Still needs a bit more work/polish/testing.
 - Requires IE8 or newer because I didn't want jQuery as a dependency.

##License

MIT

---
_Source: https://npm.io/package/reformer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
