#Cerebral Form
##Philosophy:
The inputs are first class citizens, not the "form". You determine where their state lives, and what forms, if any, they connect to.
##Known Issue:
If one input needs to be re-validated after another input changes, that can work via the "linkTo" option. The linkTo option currently only supports just 1 linked input per input.
##Usage:
npm i -S cerebral-form
import InputWrapper from 'cerebral-form/InputWrapper';
var MyInput = InputWrapper(function MyInput (props) {
return (
<input {...props}>
</input>
);
},{
path: ['path','you','choose'],
form: 'form1',
validation: {
'isEmail': "Please provide a valid email",
'userDefined': "This field must match another user defined field"
'validationWithOptions': {
anything: 'canGoHere'
message: "This field must match another user defined field"
}
},
asyncValidation: 'isNonGmail',
validateImmediately: false,
linkTo: ['anotherInput']
})
A list of available props available to the input are:
- onChange: function onChange()
- onBlur: function onBlur()
- value: "hahah"
- completed: false //false unless default value provided
- errors: {
isEmail: "Please provide a valid email"
}
- hasError: false
- visited: false
- checked: false
##Checking if a form has been completed:
import {formCompleted} from 'cerebral-form';
@Cerebral(formCompleted: formCompleted('form1')})
##Hooking up the module + setting up validation:
import CerebralForm from 'cerebral-form';
import validator from 'validator';
controller.modules({
cerebralForm: CerebralForm({
validation: {
...validator,
isEmail: function (value, state, options) {
if (value.indexOf('@') > -1) {
return true
} else {
return false
}
},
userDefined: function (value, state, options) {
if (value === state.get('userDefinedString')) {
return true
} else {
return false
}
}
validationWithOptions: function (value, state, options) {
console.log(options.anything)
return true;
}
},
asyncValidation: {
isNonGmail: [
[function({
input, output
}) {
setTimeout(function() {
if (input.value.indexOf('gmail') > -1) {
output({
asyncError: {
message: 'The email ' + input.value + ' cannot have gmail in the name'
}
})
} else {
output()
}
}, 300)
}]
]
}
}),
});