@cerebral/forms
Install
NPM
npm install @cerebral/forms
Description
The forms provider allows you to easily compute forms based on a number of rules. Easily add new rules, error messages and, if you want, add whatever you want to your fields for custom logic.
Instantiate
import {Controller} from 'cerebral'
import FormsProvider from '@cerebral/forms'
const controller = Controller({
providers: [
FormsProvider({
rules: {
myAddedRule (value, arg, get) {
value
arg
get
return true
}
},
errorMessages: {
minLength (value, minLength) {
return `The length is ${value.length}, should be equal or more than ${minLength}`
}
}
})
]
})
compute
To use a form you use the form computed, pointing to the form. Typically:
import React from 'react'
import {connect} from '@cerebral/react'
import {form} from '@cerebral/forms'
export default connect({
form: form(state`path.to.form`)
},
function MyForm ({form}) {
form.someField.value
form.someField.hasValue
form.someField.isPristine
form.someField.isValid
form.someField.failedRule.name
form.someField.failedRule.arg
form.someField.errorMessage
form.getInvalidFields()
form.getFields()
}
)
You can also use the field computed, pointing to the field. This will optimize rendering as only the field will render on change.
import React from 'react'
import {connect} from '@cerebral/react'
import {field} from '@cerebral/forms'
export default connect({
field: field(state`path.to.form.name`)
},
function MyField ({field}) {
field.value
field.hasValue
field.isPristine
field.isValid
}
)
defaultValue
You can define a default value for your fields. When the form is reset, it will put back the default value:
{
myForm: {
firstName: {
value: '',
defaultValue: 'Ben'
}
}
}
field
A field is just an object with a value
property:
{
myForm: {
myField: {
value: ''
}
}
}
form
A form is just an object in the state tree:
{
myForm: {}
}
isRequired
Define field as required. This will make the field invalid if there is no value. By default forms identifies a value or not
using the isValue rule. You can change this rule if you want, look below.
{
myForm: {
firstName: {
value: '',
isRequired: true
}
}
}
isValueRules
You can change what defines a field as having a value. For example if your value is an array, you can use the minLength rule to
define a required minimum of 3 items in the array.
{
myForm: {
interests: {
value: [],
isRequired: true,
isValueRules: ['minLength:3']
}
}
}
nesting
You can nest this however you want, even with array:
{
myForm: {
firstName: {value: ''},
lastName: {value: ''},
address: [{
street: {value: ''},
zipCode: {value: ''}
}],
interests: {
books: {value: false},
films: {value: false}
}
}
}
operators
isValidForm
Diverge execution based on validity of a form.
import {state} from 'cerebral/tags'
import {isValidForm} from '@cerebral/forms/operators'
export default [
isValidForm(state`my.form`), {
true: [],
false: []
}
]
resetForm
Reset a form.
import {state} from 'cerebral/tags'
import {resetForm} from '@cerebral/forms/operators'
export default [
resetForm(state`my.form`)
]
setField
When you change the value of a field you will need to use this operator. Note that you point to the field, not the field value.
import {state, props} from 'cerebral/tags'
import {setField} from '@cerebral/forms/operators'
export default [
setField(state`my.form.field`, props`value`)
]
provider
You can also access your forms in actions.
function myAction ({forms}) {
const form = forms.get('path.to.form')
}
reset
Reset the form to its default values (or empty string by default).
function myAction ({forms}) {
forms.reset('path.to.form')
}
toJSON
Typically you want to convert your forms to a plain value structure.
function myAction ({forms}) {
const form = forms.toJSON('path.to.form')
}
This form will now have the structure of:
{
myField: 'theValue',
address: {
street: 'street value',
zipCode: 'zip code value'
}
}
updateErrorMessages
Dynamically update global error messages:
function myAction ({forms}) {
forms.updateErrorMessages({
someRule () {}
})
}
updateRules
Dynamically update available rules:
function myAction ({forms}) {
forms.updateRules({
someNewRule () {}
})
}
validationRules
You add validation rules on the field:
{
myForm: {
firstName: {
value: '',
validationRules: ['minLength:3']
}
}
}
equals:Value
{
field1: {
value: 123,
validationRules: ['equals:123']
},
field2: {
value: '123',
validationRules: ['equals:"123"']
},
field3: {
value: [],
validationRules: ['equals:[]']
}
}
equalsField:Field
{
field1: {
value: 'foo',
validationRules: ['equalsField:full.path.to.form.field2']
},
field2: {
value: 'foo',
validationRules: ['equalsField:full.path.to.form.field1']
},
field3: {
value: 'bar',
validationRules: ['equalsField:full.path.to.form.field2']
}
}
isAlpha
{
field1: {
value: 'abc',
validationRules: ['isAlpha']
},
field2: {
value: 'AbC',
validationRules: ['isAlpha']
},
field3: {
value: '123abc',
validationRules: ['isAlpha']
}
}
isAlphanumeric
{
field1: {
value: '123abc',
validationRules: ['isAlphanumeric']
},
field2: {
value: '123',
validationRules: ['isAlphanumeric']
},
field3: {
value: '123+abc',
validationRules: ['isAlphanumeric']
}
}
isEmail
{
field1: {
value: 'ho@hep.co',
validationRules: ['isEmail']
},
field2: {
value: 'hello@',
validationRules: ['isEmail']
},
field3: {
value: 'hel.co',
validationRules: ['isEmail']
}
}
isEmpty
{
field1: {
value: '',
validationRules: ['isEmpty']
},
field2: {
value: 'hello',
validationRules: ['isEmpty']
},
field3: {
value: 123,
validationRules: ['isEmpty']
}
}
isExisty
{
field1: {
value: 0,
validationRules: ['isExisty']
},
field2: {
value: [],
validationRules: ['isExisty']
},
field3: {
value: null,
validationRules: ['isExisty']
}
}
isFalse
{
field1: {
value: false,
validationRules: ['isFalse']
},
field2: {
value: 'false',
validationRules: ['isFalse']
},
field3: {
value: true,
validationRules: ['isFalse']
}
}
isFloat
{
field1: {
value: '22.5',
validationRules: ['isFloat']
},
field2: {
value: 22.5,
validationRules: ['isFloat']
},
field3: {
value: '22',
validationRules: ['isFloat']
}
}
isInt
{
field1: {
value: '123',
validationRules: ['isInt']
},
field2: {
value: 123,
validationRules: ['isInt']
},
field3: {
value: '22.5',
validationRules: ['isInt']
}
}
isLength:Number
{
field1: {
value: 'hey',
validationRules: ['isLength:3']
},
field2: {
value: ['foo', 'bar'],
validationRules: ['isLength:2']
},
field3: {
value: 'hm 123',
validationRules: ['isLength:3']
}
}
isNumeric
{
field1: {
value: '123',
validationRules: ['isNumeric']
},
field2: {
value: 123,
validationRules: ['isNumeric']
},
field3: {
value: '123abc',
validationRules: ['isNumeric']
}
}
isSpecialWords
{
field1: {
value: 'hey there',
validationRules: ['isSpecialWords']
},
field2: {
value: 'some åäö',
validationRules: ['isSpecialWords']
},
field3: {
value: 'hm 123',
validationRules: ['isSpecialWords']
}
}
isTrue
{
field1: {
value: true,
validationRules: ['isTrue']
},
field2: {
value: 'true',
validationRules: ['isTrue']
},
field3: {
value: false,
validationRules: ['isTrue']
}
}
isUndefined
{
field1: {
value: undefined,
validationRules: ['isUndefined']
},
field2: {
value: 'hello',
validationRules: ['isUndefined']
},
field3: {
value: 123,
validationRules: ['isUndefined']
}
}
isUrl
{
field1: {
value: 'http://www.test.com',
validationRules: ['isUrl']
},
field2: {
value: 'http://www',
validationRules: ['isUrl']
},
field3: {
value: 'http//www',
validationRules: ['isUrl']
}
}
isWords
{
field1: {
value: 'hey there',
validationRules: ['isWords']
},
field2: {
value: 'wut åäö',
validationRules: ['isWords']
},
field3: {
value: 'hm 123',
validationRules: ['isWords']
}
}
isValue
{
field1: {
value: 'test',
validationRules: ['isValue']
},
field2: {
value: [],
validationRules: ['isValue']
},
field3: {
value: null,
validationRules: ['isValue']
},
field3: {
value: false,
validationRules: ['isValue']
}
}
maxLength:Number
{
field1: {
value: '123',
validationRules: ['maxLength:3']
},
field2: {
value: 'fo',
validationRules: ['maxLength:3']
},
field3: {
value: ['foo', 'bar', 'baz', 'mip'],
validationRules: ['maxLength:3']
}
}
minLength:Number
{
field1: {
value: '123',
validationRules: ['minLength:3']
},
field2: {
value: 'fo',
validationRules: ['minLength:3']
},
field3: {
value: ['foo', 'bar', 'baz', 'mip'],
validationRules: ['minLength:3']
}
}
regexp
{
field1: {
value: 'foo',
validationRules: [/foo/]
},
field2: {
value: 'bar',
validationRules: [/foo/]
}
}