React Advanced Form
  • Introduction
  • General
    • Comparison
      • Compared to Formik
      • Compared to Final form
    • Migration guides
      • 1.4.x → 1.5.x
    • FAQ
  • Getting started
    • Installation
    • Creating fields
    • Creating form
    • Validation rules
    • Validation messages
    • Applying validation
    • Handle submit
  • Architecture
    • Argument properties
    • Referencing
    • Field lifecycle
    • Controlled fields
    • Reactive props
  • Validation
    • Getting started
    • Validation schema
      • Rule definition
      • Reactive rule
    • Validation messages
  • High-order components
    • createField
      • Options
      • Field presets
      • Exposed props
  • Components
    • FormProvider
    • Form
      • Props
        • innerRef
        • initialValues
        • action
        • rules
        • messages
      • Methods
        • setValues()
        • setErrors()
        • reset()
        • validate()
        • serialize()
        • submit()
      • Callbacks
        • onFirstChange
        • onReset
        • onInvalid
        • onSerialize
        • onSubmitStart
        • onSubmitted
        • onSubmitFailed
        • onSubmitEnd
    • Field.Group
    • Field
      • Props
        • rule
        • asyncRule
        • skip
      • Callbacks
        • onFocus
        • onChange
        • onBlur
  • Recipes
    • Generating a form
    • Utilizing functions
  • Developers
    • Contributing
Powered by GitBook
On this page
  • Specification
  • Parameters
  • Example
  1. Components
  2. Form
  3. Callbacks

onSerialize

Specification

A callback handler dispatched upon form serialization. Accepts the original serialized fields and return the next state of serialized fields.

Parameters

Param name

Type

Description

serialized

Object

Original serialized fields.

fields

Object

Form fields.

form

Object

Form component reference.

Example

import React from 'react'
import { Form, Field } from 'react-advanced-form'
import { Input } from 'react-advanced-form-addons'

export default class ExampleForm extends React.Component {
  /* Callback method accepts "raw" serialized object */
  mapSerialized = ({ serialized, fields, form }) => {
    const { password, billingAddress: { address  } = serialized
    const [_, street, houseNumber] = address.match(/(.+)(\d+)/)
    
    return {
      ...serialized,
      /* Transforms the serialized value of "password" field */
      password: btoa(password),
      
      /* Beware to preserve field group nested keys */
      billingAddress: {
        street,
        houseNumber,
      },
    }
  }
  
  /* Any subsequent handlers accept transformed "serialized" object */
  registerUser = ({ serialized, fields, form }) => {
    console.log(serialized) // { "username": "admin", "password": "..." }
  }
  
  render() {
    <Form
      action={this.registerUser}
      onSerialize={this.mapSerialized}>
      <Input
        name="username"
        label="Username"
        initialValue="admin"
        required />
      <Input
        name="password"
        label="Password"
        initialValue="foo"
        required />
      <Field.Group name="billingAddress">
        <Input
          name="address"
          initialValue="Baker st. 12" />
      </Field.Group>
    </Form>
  }
}
PreviousonInvalidNextonSubmitStart

Last updated 6 years ago