简体   繁体   English

表单未使用 react 和 formik 提交

[英]form is not submitting using react and formik

Below is my Model (popup) code to send user email address to backend service.下面是我的 Model(弹出)代码,用于将用户 email 地址发送到后端服务。 I have rendered this Model component in my Login Component.我在我的登录组件中渲染了这个 Model 组件。 I am not able to submit this form.我无法提交此表格。 I don't know what i am missing here but my other forms are working fine.我不知道我在这里缺少什么,但我的其他 forms 工作正常。 My Yup validations are working fine but when i click on "send" button, its not going inside onSubmit handler even if the field is validated.我的 Yup 验证工作正常,但是当我单击“发送”按钮时,即使该字段已验证,它也不会进入 onSubmit 处理程序。

    import React from 'react';
    import { Formik, Field, Form, ErrorMessage } from 'formik';
    import * as Yup from 'yup';
    import { errorMessage } from '../../utility/error-messages';
    import { Button, Modal, ModalBody, ModalFooter } from 'reactstrap';

    const TextFieldComponent = (props) => {
    return (
        <div className="formGroup">
        {props.touched &&
        props.touched[props.name] &&
        props.errors &&
        props.errors[props.name] !== undefined ? (
            <ErrorMessage
            name={props.name}
            render={(msg) => <label className="errorMessage">{msg}</label>}
            />
        ) : (
            <label htmlFor={props.name}>{props.label}</label>
        )}

        <Field
            name={props.name}
            type="text"
            className={
            props.touched &&
            props.touched[props.name] &&
            props.errors &&
            props.errors[props.name] !== undefined
                ? 'formControl error '
                : 'formControl'
            }
            onBlur={props.handleBlur}
            onChange={props.handleChange}
        />
        </div>
    );
    };

    const setSchema = Yup.object({
    email: Yup.string()
        .email(errorMessage.emailValidation)
        .required(errorMessage.emailRequired),
    });

    export const ForgetPasswordModal = ({ show = false, onClose = () => {} }) => {
    debugger;
    return (
        <>
        <Formik
            initialValues={{
            email: '',
            }}
            validationSchema={setSchema}
            onSubmit={(values, { setSubmitting }) => {
            setTimeout(() => {
                alert(JSON.stringify(values, null, 2));
                setSubmitting(false);
            }, 400);
            }}
        >
            {({ isSubmitting, errors, touched, handleChange, handleBlur }) => {
            return (
                <>
                <Form>
                    <Modal
                    className="forgetPassPopup resetPassword"
                    isOpen={show}
                    backdrop={'static'}
                    centered
                    fade
                    >
                    <ModalBody>
                        <h3>Reset password</h3>
                        <p>
                        Enter the email.
                        </p>
                        <div className="formGroup">
                        <TextFieldComponent
                            name="email"
                            label="email"
                            errors={errors}
                            touched={touched}
                            handleBlur={handleBlur}
                            handleChange={handleChange}
                        />
                        </div>
                    </ModalBody>
                    <ModalFooter>
                        <Button
                        className="btn btnSecondary"
                        onClick={() => onClose(false)}
                        >
                        Cancel
                        </Button>
                        <Button
                        type="submit"
                        disabled={isSubmitting}
                        className="btn btnPrimary"
                        >
                        Send
                        </Button>
                    </ModalFooter>
                    </Modal>
                </Form>
                </>
            );
            }}
        </Formik>
        </>
    );
    };

It may be due to the Modal component.这可能是由于 Modal 组件。 The modal is inside the form and if portal is used to render the modal it may be rendered outside the form.模态在表单内部,如果门户用于呈现模态,它可能会呈现在表单外部。 Can you try using form inside the modal and check if it works.您可以尝试在模式中使用表单并检查它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM