亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Formik 驗證 isSubmitting / isValidating 未設置為 true

Formik 驗證 isSubmitting / isValidating 未設置為 true

holdtom 2023-07-20 16:19:45
我有一個用戶要求的表格,我非常明顯地表明該表格無效。因此,我計劃彈出一個sweetalert對話框,讓他們知道他們需要仔細檢查表單。我想我可以在驗證中這樣做,以便在提交嘗試失敗時提醒他們:const validate = values => {    console.log(formik.isSubmitting); // always prints false    console.log(formik.isValidating); // always prints false    const errors = {};    if (!values.name) {      errors.name = 'Required';    }    if (Object.keys(errors).length > 0 && formik.isSubmitting) {        Swal.fire({            icon: 'error',            title: "Oops. . .",            text: "There are errors with the form. Please double check your options.",            footer: "<div>Problems: " + Object.keys(errors).join(', ') + "</div>"        })    }    return errors;};const formik = useFormik({    initialValues: {        name: item.name    },    enableReinitialize: true,    validate,    onSubmit: values => {       // also tried adding        formik.setSubmitting(true);        //do stuff    }})但它們isSubmitting / isValidating總是假的。我是否需要向函數傳遞額外的屬性validate才能訪問這些值?https://codesandbox.io/s/nervous-wescoff-cf2y1?file=/src/App.js
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

我相信該validate方法不是向用戶顯示對話框的好地方。您的用例看起來像是 formik lib 的自定義需求。

import React, { useState } from "react";

import "./styles.css";

import { useFormik } from "formik";

import Swal from "sweetalert2";

import "bootstrap/dist/css/bootstrap.min.css";


export default function App() {

? const [item, setItem] = useState({

? ? name: "",

? ? email: ""

? });


? const validate = (values) => {

? ? console.log("values: ", values);

? ? const errors = {};

? ? if (!values.name) {

? ? ? errors.name = "Required";

? ? }

? ? return errors;

? };


? const initialValues = {

? ? name: item.name,

? ? email: item.email

? };


? const formik = useFormik({

? ? initialValues,

? ? enableReinitialize: true,

? ? validate,

? ? onSubmit: (values) => {

? ? ? console.log("inside onSubmit", values);

? ? }

? });


? const customSubmitHandler = (event) => {

? ? event.preventDefault();

? ? const touched = Object.keys(initialValues).reduce((result, item) => {

? ? ? result[item] = true;

? ? ? return result;

? ? }, {});

? ? // Touch all fields without running validations

? ? formik.setTouched(touched, false);

? ? formik.setSubmitting(true);


? ? formik

? ? ? .validateForm()

? ? ? .then((formErrors) => {

? ? ? ? if (Object.keys(formErrors).length > 0) {

? ? ? ? ? Swal.fire({

? ? ? ? ? ? icon: "success",

? ? ? ? ? ? title: "Yes. . .",

? ? ? ? ? ? text: "This one should fire if everything is working right",

? ? ? ? ? ? footer:

? ? ? ? ? ? ? "<div>Problems: " + Object.keys(formErrors).join(", ") + "</div>"

? ? ? ? ? });

? ? ? ? } else {

? ? ? ? ? formik.handleSubmit(event);

? ? ? ? }

? ? ? ? formik.setSubmitting(false);

? ? ? })

? ? ? .catch((err) => {

? ? ? ? formik.setSubmitting(false);

? ? ? });

? };


? return (

? ? <form id="campaignForm" onSubmit={customSubmitHandler}>

? ? ? <div className="form-group">

? ? ? ? <label htmlFor="name">Name</label>

? ? ? ? <input

? ? ? ? ? id="name"

? ? ? ? ? type="text"

? ? ? ? ? onChange={formik.handleChange}

? ? ? ? ? value={formik.values.name}

? ? ? ? ? className="form-control"

? ? ? ? ? placeholder="Enter name"

? ? ? ? />

? ? ? ? {formik.errors.name ? (

? ? ? ? ? <div className="text-danger">{formik.errors.name}</div>

? ? ? ? ) : null}

? ? ? </div>

? ? ? <div className="form-group">

? ? ? ? <label htmlFor="name">Email</label>

? ? ? ? <input

? ? ? ? ? id="name"

? ? ? ? ? type="email"

? ? ? ? ? onChange={formik.handleChange}

? ? ? ? ? value={formik.values.email}

? ? ? ? ? className="form-control"

? ? ? ? ? placeholder="Enter email"

? ? ? ? />

? ? ? ? {formik.errors.email ? (

? ? ? ? ? <div className="text-danger">{formik.errors.email}</div>

? ? ? ? ) : null}

? ? ? </div>

? ? ? <div className="form-group">

? ? ? ? <button className="btn btn-info" type="submit">

? ? ? ? ? Submit

? ? ? ? </button>

? ? ? </div>

? ? </form>

? );

}


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號