Socket
Socket
Sign inDemoInstall

validated-form

Package Overview
Dependencies
26
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    validated-form

Helpers for creating a form with validated fields.


Version published
Maintainers
2
Install size
5.27 MB
Created

Readme

Source

validated-form

Helpers for creating a form with validated fields.

ValidatedForm

Properties

  • fields An object of field names and options {name: options, ...}. See field options for more information
  • values An object of field values {name: value, ...}
  • onSubmit(values) A function to be called when the form is submitted (only if fields are valid)

Example

<ValidatedForm ref='form' fields={
    city: {}
    state: {placeholder: 'e.g. "NY" or "MA"'}
    county: {optional: true}
} onSubmit={({city, state, county}) -> alert(city + ' is in ' + state) } />

ValidatedField

Properties

  • name Name of this field
  • type One of "text", "number", "email", or "toggle", default is "text"
  • placeholder A placeholder, default is the un-slugified field name
  • error_message Message to show if field is invalid
  • hidden A boolean or function to determine if this field is hidden
  • optional A boolean or function to determine if this field is optional

ValidatedFormMixin

The ValidatedFormMixin provides methods for rendering fields, keeping track of fields state, and checking that fields are valid.

Class attributes

  • getInitialState() -> {values} required
    • A component that uses this mixin must define a getInitialState that returns at least an empty values: {}, because a React component does not have state by default
    • You can also use this to pre-fill values of fields, with e.g. values: {email: 'test@gmail.net'}
  • getDefaultProps() -> {fields, onSubmit}
    • Define fields and other props here, or pass them in as props instead
  • onSubmit(values)
    • A function to be called when the form is submitted (only if fields are valid)
    • Can be defined directly on your class instead of as a property

Properties

  • fields An object of field names and options in the shape {name: options, ...}. See field options for more information
  • onSubmit(values) A function to be called when the form is submitted (only if fields are valid)

State

  • values An object of values by field name

Methods from mixin

  • renderField(name) Render an individual field with the options in fields[name]
  • trySubmit() A method that looks through fields
  • clear() Sets all field values to null

Example

{ValidatedFormMixin} = require 'validated-form'

FormTest = React.createClass
    mixins: [ValidatedFormMixin]

    getInitialState: ->
        values:
            name: 'test'

    getDefaultProps: ->
        fields:
            name: {type: 'text'}
            age: {type: 'number'}
            email: {type: 'email'}

    onSubmit: (values) ->
        @setState {loading: true}
        submitFormAnd =>
            @clear()
            @setState {loading: false}

    render: ->
        <div>
            <form onSubmit=@trySubmit>
                {@renderField 'name'}
                {@renderField 'age'}
                {@renderField 'email'}
                <button>{if @state.loading then "Loading..." else "Submit"}</button>
            </form>
        </div>

FAQs

Last updated on 25 Jul 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc