New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

recassfov

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recassfov

React client and server side form validation

  • 0.0.29
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

recassfov

npm license

React Client and Server Side Form Validation

Demo

Usage

Install library.

# with yarn
$ yarn add recassfov

# or with npm
$ npm i recassfov

Import library.

import { Form, Input } from 'recassfov'

Create validation rules. (Validator.js)

const validations = {
  username: [
    {
      rule: 'isLength',
      args: { min: 4, max: 32 },
      invalidFeedback: 'please provide a username (min: 4, max: 32)'
    }
  ],
  email: [
    {
      rule: 'isEmail',
      invalidFeedback: 'please provide a valid email'
    }
  ],
  message: [
    {
      rule: 'isLength',
      args: { min: 1 },
      invalidFeedback: 'please provide a message'
    }
  ]
}

Build your form.

<Form postUrl='http://site.com/post'>
  <div>
    <Input
      type='text'
      name='username'
      placeholder='username'
      validations={validations.username}
      />
  </div>

  <div>
    <Input
      type='email'
      name='email'
      placeholder='email'
      validations={validations.email}
      />
  </div>

  <div>
    <Textarea
      name='message'
      placeholder='message'
      validations={validations.message}
      />
  </div>

  <div>
    <input type='submit' value='submit' />
  </div>
</Form>

Add .is-invalid and .invalid-feedback classes into your CSS.

.is-invalid {
  border: 1px solid #dc3545;
}
.invalid-feedback {
  display: none;
  color: #dc3545;
}
.is-invalid~.invalid-feedback {
  display: block;
}

Make sure you add the errors to the validations object in backend.

app.post('/signup', (req, res) => {
  const result = {
    validations: {}
  }

  if (req.body.username === 'john') {
    result.validations.username = 'john is already registered'
  }

  res.send(result)
})

Props & Callbacks

<Form>

Props

<Form
  postUrl='http://site.com.post'
  headers={{
    'Content-Type': 'application/json'
  }}
  classNames={{
    invalidInput: 'is-invalid',
    invalidFeedback: 'invalid-feedback'
  }}
  >

Callbacks

<Form
  onSubmit={() => {
    console.log('onSubmit')
  }}
  validFormBeforePost={(res) => {
    console.log(res.formItems)
  }}
  invalidFormBeforePost={(res) => {
    console.log(res.formItems)
  }}
  validFormAfterPost={(res) => {
    console.log(res.formItems)
    console.log(res.ajaxResult)
    res.cleanFormItems()
  }}
  invalidFormAfterPost={(res) => {
    console.log(res.formItems)
    console.log(res.ajaxResult)
  }}
  >

Contribution

Feel free to contribute. Open a new issue, or make a pull request.

License

MIT

Keywords

FAQs

Package last updated on 21 Aug 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc