IntroductionInstallationFormSimple FormButtonFieldFieldWrapperRadioGroupSelectForm ValidationStateMeta DescriptiononFocus onChange onBlurState ManipulationCustom Error MessagesFeedback on disabled ButtonStylingDynamic FieldsDynamic Fields 2Dynamic Fields 3Bind Input FieldsBind Input Fields 2Third Party ComponentsFile UploadWhy
State
Working with the state data and meta data
A simple Example of how your Form could look like:
<Form><Field id="email" type="email" required /><Field id="password" type="password" required /><Buttonid="button"type="submit"onClick={(state) => this.handleOnClick(state)}>Submit</Button></Form>
You can also use
onFocus
,onChange
, andonBlur
on everyField
,Select
, orRadioGroup
components to get access to the state of the form (more info)
As you can see the <Button />
component has a onClick
property. This property takes a function. The handleOnClick
function could look something like:
async handleOnClick(state) {// do something with the state// e.g. send data to a servertry {const response = await fetch('https://server.com/login', {method: 'POST',headers: {'Content-Type': 'application/json; charset=utf-8',},body: JSON.stringify(state.data);});// do something with the response} catch(error) {// do something with the error}}
As you can see the state
parameter is an object. It has three different properties.
- data:
object
- meta:
object
- formValid:
boolean
In our case data
would hold:
{"data": {"email": "","password": ""}}
meta
would hold:
{"meta": {"email": {"initialValue": "","dirty": false,"pristine": true,"visited": false,"touched": false,"value": "","valid": false,"invalid": true,"rules": {"type": "email","min": undefined,"max": undefined,"sameAs": undefined,"match": undefined,"required": true,}},"password": {"initialValue": "","dirty": false,"pristine": true,"visited": false,"touched": false,"value": "","valid": false,"invalid": true,"rules": {"type": "password","min": undefined,"max": undefined,"sameAs": undefined,"match": undefined,"required": true,}}}}
formValid
would hold:
{"formValid": false}
Meta Description
Meta data gives you an overview of what happened on that field. E.g. if a field is pristine
you could deside to not send its data to the server since, nothing changed.
Property | Type | Description |
---|---|---|
initialValue | String or Bool | |
dirty | Bool | true if the value !== initialValue |
pristine | Bool | true if the value === initialValue |
visited | Bool | true if this field was focused (onFocus ) |
touched | Bool | true if this field was blurred (onBlur ) |
value | String or Bool | |
valid | Bool | true if this field is valid (passed all rules) |
invalid | Bool | true if this field is invalid (failed at least one rules) |
rules | object | the rules for the validation of this field |
rules.type | String | checkbox , date , textarea , datetime-local , email , number , tel , text , password , time , url , radio , select |
rules.min | Number or String (YYYY-MM-DD) | text , textarea , password : this field has to have at least min characters; number , date : this field has to be at least min |
rules.max | Number or String (YYYY-MM-DD) | text , textarea , password : this field has to have maximum max characters; number , date : this field has to be maximum max |
rules.match | RegEx | thie fields has to match the regular expression |
rules.sameAs | String | thie fields has to have the same value as the field with id sameAs (e.g. password fields) |
rules.required | Bool | this field is required (has to have a value) |