recassfov

0.0.35 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.350latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.350
0.0.340
0.0.330
0.0.320
0.0.310
0.0.301
0.0.290
0.0.282
0.0.270
0.0.260
0.0.250
0.0.240
0.0.231
0.0.220
0.0.211
0.0.200
0.0.190
0.0.180
0.0.170
0.0.160
0.0.151
0.0.140
0.0.131
0.0.121
0.0.110
0.0.101
0.0.81
0.0.70
0.0.61
0.0.50
0.0.40
0.0.20
0.0.11

Package Sidebar

Install

npm i recassfov

Weekly Downloads

12

Version

0.0.35

License

MIT

Unpacked Size

286 kB

Total Files

12

Last publish

Collaborators

  • ozgrozer