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

final-form-set-errors-mutator

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

final-form-set-errors-mutator

Mutator for setting errors outside of form (for example in redux-saga)

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

final-form-set-errors-mutator

Mutator for setting errors outside of form (for example in redux-saga)

How to use

Add to form

import { FormApi, FormSubscription } from "final-form";
import React, { FunctionComponent, useState, ChangeEvent } from "react";
import { Form, Field } from "react-final-form";
import { setErrors } from "final-form-set-errors-mutator";
import { TextField } from "mui-fields";
import { ModelState } from "model-state-validation";

export interface IMyFormProps {
    submit: (form: any) => void;
}

type Props = IMyFormProps;

const MyForm: FunctionComponent<Props> = (props) => {
    const [formApi, setFormApi] = useState<FormApi>(null);

    const formSubscription: FormSubscription = {
        submitting: true,
    };
    
    const [password, setPassword] = useState<string>("");
    const [confirmPassword, setConfirmPassword] = useState<string>("");

    const handleChangePassword = (event: ChangeEvent<HTMLInputElement>) => {
        const modelState = new ModelState();
        if (event.target.value !== confirmPassword) {
            modelState.addError(
                "confirm-password",
                "Passwords are not equals."
            );
        }
        setPassword(event.target.value);

        formApi.mutators.setErrors(modelState);
    };

    const handleChangeConfirmPassword = (event: ChangeEvent<HTMLInputElement>) => {
        const modelState = new ModelState();
        if (password !== event.target.value) {
            modelState.addError(
                "confirmPassword",
                "Passwords are not equals."
            );
        }
        setConfirmPassword(event.target.value);

        formApi.mutators.setErrors(modelState);
    };

    return (
        <Form
            onSubmit={props.submit}
            subscription={formSubscription}
            mutators={[
                setErrors,
            ]}
        >
            {({ handleSubmit, form }) => {
                setFormApi(form);

                return (
                    <form onSubmit={handleSubmit}>
                        <Field
                            name={"password"}
                            component={TextField}
                            type="password"
                            onChange={handleChangePassword}
                        />
                        <Field
                            name={"confirm-password"}
                            component={TextField}
                            type="password"
                            onChange={handleChangeConfirmPassword}
                        />
                    </form>
                );
            }}
        </Form>
    );
};

export { MyForm };

FAQs

Package last updated on 27 Apr 2020

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