npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

newforms

v0.13.2

Published

An isomorphic form-handling library for React

Downloads

270

Readme

newforms travis status

An isomorphic form-handling library for React.

(Formerly a direct port of the Django framework's django.forms library)

Getting newforms

Node.js

Newforms can be used on the server, or bundled for the client using an npm-compatible packaging system such as Browserify or webpack.

npm install newforms
var forms = require('newforms')

By default, newforms will be in development mode. To use it in production mode, set the environment variable NODE_ENV to 'production' when bundling. To completely remove all development mode code, use a minifier that performs dead-code elimination, such as UglifyJS.

Browser bundle

The browser bundle exposes a global forms variable and expects to find global React variable to work with.

The uncompressed bundle is in development mode, so will log warnings about potential mistakes.

You can find it in the /dist directory.

Upgrade Guide

Documentation @ ReadTheDocs

Newforms Examples @ GitHub

Related Projects

Other React Form Libraries

Quick Guide

A quick introduction to defining and using newforms Form objects.

Design your Form

The starting point for defining your own forms is Form.extend().

Here's a simple (but incomplete!) definition of a type of Form you've probably seen dozens of times:

var SignupForm = forms.Form.extend({
  username: forms.CharField(),
  email: forms.EmailField(),
  password: forms.CharField({widget: forms.PasswordInput}),
  confirmPassword: forms.CharField({widget: forms.PasswordInput}),
  acceptTerms: forms.BooleanField({required: true})
})

A piece of user input data is represented by a Field, groups of related Fields are held in a Form and a form input which will be displayed to the user is represented by a Widget. Every Field has a default Widget, which can be overridden.

Rendering a Form

Forms provide helpers for rendering labels, user inputs and validation errors for their fields. To get you started quickly, newforms provides a React component which use these helpers to render a basic form structure.

At the very least, you must wrap rendered form contents in a <form>, provide form controls such as a submit button and hook up handling of form submission:

var Signup = React.createClass({
  render: function() {
    return <form onSubmit={this._onSubmit}>
      <forms.RenderForm form={SignupForm} ref="signupForm"/>
      <button>Sign Up</button>
    </form>
  },

  // ...

Rendering helpers attach event handlers to the inputs they render, so getting user input data is handled for you.

The RenderForm component handles creating a form instance for you, and setting up automatic validation of user input as it's given.

To access this form instance later, make sure the component has a ref name.

Handling form submission

The final step in using a Form is validating when the user attempts to submit.

First, use the ref name you defined earlier to get the form instance via the RenderForm component's getForm() method.

Then call the form's validate() method to ensure every field in the form is validated against its current user input.

If a Form is valid, it will have a cleanedData object containing validated data, coerced to the appropriate JavaScript data type when appropriate:

  propTypes: {
    onSignup: React.PropTypes.func.isRequired
  },

  _onSubmit: function(e) {
    e.preventDefault()

    var form = this.refs.signupForm.getForm()
    var isValid = form.validate()
    if (isValid) {
      this.props.onSignup(form.cleanedData)
    }
  }
})

Implementing custom validation

There's an obvious validation not being handled by our form: what if the passwords don't match?

This is a cross-field validation. To implement custom, cross-field validation add a clean() method to the Form definition:

clean: function() {
  if (this.cleanedData.password &&
      this.cleanedData.confirmPassword &&
      this.cleanedData.password != this.cleanedData.confirmPassword) {
    throw forms.ValidationError('Passwords do not match.')
  }
}

Live Quickstart Demo

MIT Licensed