Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

final-form

Package Overview
Dependencies
Maintainers
1
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

final-form

🏁 Framework agnostic, high performance, subscription-based form state management

  • 4.20.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
404K
increased by0.75%
Maintainers
1
Weekly downloads
 
Created

What is final-form?

Final Form is a framework-agnostic library for managing form state in JavaScript. It provides a simple API for handling form validation, submission, and state management, making it easier to build complex forms with minimal boilerplate code.

What are final-form's main functionalities?

Form State Management

Final Form provides a simple way to manage form state. The `Form` component handles the form submission, while the `Field` component is used to define form fields. The form state is managed internally and can be accessed via the `render` prop.

const { Form, Field } = require('react-final-form');

const MyForm = () => (
  <Form
    onSubmit={formValues => {
      console.log(formValues);
    }}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="firstName" component="input" placeholder="First Name" />
        <Field name="lastName" component="input" placeholder="Last Name" />
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Validation

Final Form allows you to easily add validation to your forms. The `validate` function is used to define validation rules, and it returns an object containing any validation errors. These errors are then displayed in the form.

const { Form, Field } = require('react-final-form');

const validate = values => {
  const errors = {};
  if (!values.firstName) {
    errors.firstName = 'Required';
  }
  if (!values.lastName) {
    errors.lastName = 'Required';
  }
  return errors;
};

const MyForm = () => (
  <Form
    onSubmit={formValues => {
      console.log(formValues);
    }}
    validate={validate}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="firstName" component="input" placeholder="First Name" />
        <Field name="lastName" component="input" placeholder="Last Name" />
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Field-Level Validation

Final Form supports field-level validation, allowing you to define validation rules for individual fields. The `validate` prop on the `Field` component is used to specify the validation function for that field.

const { Form, Field } = require('react-final-form');

const required = value => (value ? undefined : 'Required');

const MyForm = () => (
  <Form
    onSubmit={formValues => {
      console.log(formValues);
    }}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="firstName" component="input" placeholder="First Name" validate={required} />
        <Field name="lastName" component="input" placeholder="Last Name" validate={required} />
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Form Submission

Final Form makes it easy to handle form submission. The `onSubmit` prop on the `Form` component is used to define the submission handler, which receives the form values as an argument.

const { Form, Field } = require('react-final-form');

const MyForm = () => (
  <Form
    onSubmit={formValues => {
      console.log('Form submitted with values:', formValues);
    }}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="firstName" component="input" placeholder="First Name" />
        <Field name="lastName" component="input" placeholder="Last Name" />
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Other packages similar to final-form

FAQs

Package last updated on 23 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc