简体   繁体   English

Formik React 使用 2 个按钮(提交和保存)提交表单 - 保存按钮不触发验证

[英]Formik React with 2 buttons (Submit and Save) to submit form - Save button not to trigger validation

Pretty new with Formik, I have a simple form, which has validation. Formik 很新,我有一个简单的表单,它有验证。 I need to have 2 buttons, Submit and a Save button which will mostly do the same thing, however, if "Save" button is clicked, I want the validation to be "disabled" or rather, all required fields will no longer be required.我需要有 2 个按钮,提交和一个保存按钮,它们主要做同样的事情,但是,如果单击“保存”按钮,我希望验证被“禁用”或者更确切地说,不再需要所有必填字段. Any ideas how I can achieve this?有什么想法可以实现吗?

Some codes below:下面的一些代码:

const initialValues = {
    title: "",
    description: ""
};

const validationSchema = Yup.object().shape({
        title: Yup.string()
            .max(50, 'You may only enter up to 50 characters')
            .required('Required'),
        description: Yup.string()
            .max(200, 'You may only enter up to 200 characters')
            .required('Required'),
        })


const CreateForm = () => {

    const handleCancel = () => {
        alert("Cancelled!")
    }

    return (
        <div>
            <Formik initialValues={initialValues}
                validationSchema={validationSchema}
                onSubmit={(values) => {
                    setTimeout(() => {
                        alert(JSON.stringify(values, null, 2));
                    }, 3000)
                }}
            >
                {props => (
                    <Form>
                        <CustomTextInput label="Title"
                            name="title" type="input" placeholder="Enter Title" />

                        <CustomTextInput label="Description"
                            name="description" type="input" placeholder="Description" />

                        <div>
                            <Button type="submit" variant="contained" color="primary">
                                Submit
                            </Button> &nbsp;
                            <Button type="submit" variant="contained" color="secondary" >
                                Save
                            </Button>&nbsp;
                            <Button variant="contained" color="default" onClick={handleCancel}>
                                Cancel
                            </Button>
                        </div>
                    </Form>
                )}
            </Formik>
        </div>
    )
}

export default CreateForm

Firstly remove type="submit" .首先删除type="submit" Because formik will understand it like submit and will validate it.因为formik会像提交一样理解它并验证它。 Second add onClick function:第二次添加onClick function:

<Button
  variant="contained"
  color="secondary"
  onClick={() => handleSave(props.values)} >Save</Button>

And:和:

const handleSave = (values) => {
  // do what you want like on submit
}

Create a variable创建一个变量

  const [btnClicked, setBtnClicked] = React.useState('');

Add the onClick event to buttons setting a value将 onClick 事件添加到设置值的按钮

              <Button type="submit" onClick={(event)=>{
                   setBtnClicked('save');
              }}> Save </Button>

              <Button type="submit" onClick={(event)=>{
                  setBtnClicked('save_and_close');
              }}>Save and close </Button>

On the formik submit event you can get the value of the button selected to do whatever you want在 formik 提交事件中,您可以获得所选按钮的值以执行任何您想做的事情

    onSubmit: (values) => {
      console.log(btnClicked);
    }

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

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