React Changeset
It provides a react hook on top of validated-changeset library (which is base library for ember-changeset
)
It makes forms easy to handle
Quick Start
For full react form codesandbox example, view
Features
- Check if form
isValid
, isDirty
(changed), changes
(what fieds are changed), errors
(error messages) without any extra logic - Define validations for each form field in declarative way (based on ember-changeset-validations)
Steps to use
- Define initial values of your form with all fields
const user = {
name: '',
email: '',
password: '',
confirmPassword: '',
};
- Pass it to changeset hook to get changeset
import { useChangeset, lookupValidator } from 'react-changeset';
const userChangeset = useChangeset(user);
const userChangeset = useChangeset(
user,
lookupValidator(validationMap),
validationMap
);
Example validation map here
- Bind changeset to react form fields
<input
type="text"
value={userChangeset.get('name')}
onChange={({ target }) => {
userChangeset.set('name', target.value);
}}
/>
- Enjoy changeset features, like
- Get only changed fields to send for PUT/PATCH request
userChangeset.changes;
- Enable/Disable submit button
<input
type="submit"
disabled={!(userChangeset.isDirty && userChangeset.isValid)}
/>
- Show errors for each field
userChangeset.error.username && userChangeset.error.username.validation[0];
For complete changeset API, visit validated-changeset
For full react form example, view
Validation Map
import {
validateLength,
validateFormat,
validatePresence,
validateConfirmation,
} from 'react-changeset';
const validationMap = {
name: [validateLength({ min: 8 })],
email: [validateFormat({ type: 'email' })],
password: [validateLength({ min: 5 }), validatePresence(true)],
confirmPassword: [validateConfirmation({ on: 'password' })],
};
export default validationMap;
Available Validation Functions
presence
Validates presence/absence of a value.
{
propertyName: validatePresence(true),
propertyName: validatePresence(false)
propertyName: validatePresence({ presence: true })
propertyName: validatePresence({ presence: true, ignoreBlank: true })
propertyName: validatePresence({ presence: true, message: "Property not present" })
}
- All validators accepts
message
property for custom error message
length
Validates the length of a String
{
propertyName: validateLength({ min: 1 }),
propertyName: validateLength({ max: 8 }),
propertyName: validateLength({ min: 1, max: 8 }),
propertyName: validateLength({ is: 16 }),
}
number
Validates various properties of a number.
{
propertyName: validateNumber({ is: 16 }),
propertyName: validateNumber({ allowBlank: true }),
propertyName: validateNumber({ integer: true }),
propertyName: validateNumber({ lt: 10 }),
propertyName: validateNumber({ lte: 10 }),
propertyName: validateNumber({ gt: 5 }),
propertyName: validateNumber({ gte: 10 }),
propertyName: validateNumber({ positive: true }),
propertyName: validateNumber({ odd: true }),
propertyName: validateNumber({ even: true }),
propertyName: validateNumber({ multipleOf: 7 })
}
format
Validates a String
based on a regular expression.
{
propertyName: validateFormat({ allowBlank: true }),
propertyName: validateFormat({ type: 'email' }),
propertyName: validateFormat({ type: 'url' }),
propertyName: validateFormat({ regex: /\w{6,30}/ })
propertyName: validateFormat({ type: 'email', inverse: true })
}
confirmation
Validates that a field has the same value as another.
{
propertyName: validateConfirmation({ on: 'password' }),
propertyName: validateConfirmation({ allowBlank: true }),
}
Custom Validators
For writing your own validators, refer 'Writing your own validators' section of ember-changeset-validations