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
  • Definition
  • Example
  1. Components
  2. Form
  3. Methods

setValues()

Sets the values of the given fields.

This method is designed to update the fields values during the runtime without making the fields controlled explicitly. You must not invoke setValues() on controlled fields.

Definition

type SetValues = (fieldsDelta: FieldsDelta) => void

type FieldsDelta = {
  [fieldPath: string]: any | FieldsDelta,
}

Example

Call the formRef.setValues() with the Object as its argument. Each key in that Object represents a field path, and each value either the next same Object, or the next value of the selected field.

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

export default class Example extends React.Component {
  handleCustomerTypeChange = () => {
    this.formRef.setValues({
      foo: 'another',
      primaryInfo: {
        firstName: 'John',
        lastName: 'Mavericl',
    })
  }
  
  render() {
    return (
      <Form ref={form => this.formRef = form}>
        <Select onChange={this.handleCustomerTypeChange}>
          <option value="b2c">Private</option>
          <option value="b2b">Business</option>
        </Select>
      
        <Input type="email" name="email" />
        <Input type="password" name="password" />
        
        <Field.Group name="primaryInfo">
          <Input name="firstName" />
          <Input name="lastName" />
        </Field.Group>
      </Form>
    )
  }
}
PreviousMethodsNextsetErrors()

Last updated 6 years ago