【问题标题】:semantic-ui-react form validation with Yup使用 Yup 进行语义 ui-react 表单验证
【发布时间】:2018-06-12 19:32:58
【问题描述】:

我读过这个solution 但这并不能真正回答这个问题。我正在使用 formik 和语义 UI 反应。 是否可以将 Yup 与语义 UI 反应一起使用? 如果可以,有人可以举个例子吗?

【问题讨论】:

    标签: semantic-ui-react formik yup


    【解决方案1】:

    是的,这是可能的,这是一种方法。将此代码粘贴到codesandbox.io 并安装依赖项,例如formik yup 和semantic-ui-react

    import React from "react";
    import { render } from "react-dom";
    import { Formik, Field } from "formik";
    import * as yup from "yup";
    import { Button, Checkbox, Form } from "semantic-ui-react";
    
    const styles = {
      fontFamily: "sans-serif",
      textAlign: "center"
    };
    
    const App = () => (
      <>
        <Formik
          initialValues={{
            firstname: "",
            lastname: ""
          }}
          onSubmit={values => {
            alert(JSON.stringify(values));
          }}
          validationSchema={yup.object().shape({
            firstname: yup.string().required("This field is required"),
            lastname: yup.string().required()
          })}
          render={({
            values,
            errors,
            touched,
            handleChange,
            handleBlur,
            handleSubmit
          }) => {
            return (
              <Form>
                <Form.Field>
                  <label>First Name</label>
                  <input
                    placeholder="First Name"
                    name="firstname"
                    onChange={handleChange}
                    onBlur={handleBlur}
                  />
                </Form.Field>
                {touched.firstname && errors.firstname && (
                  <div> {errors.firstname}</div>
                )}
                <Form.Field>
                  <label>Last Name</label>
                  <input
                    placeholder="Last Name"
                    name="lastname"
                    onChange={handleChange}
                    onBlur={handleBlur}
                  />
                </Form.Field>
                {touched.lastname && errors.lastname && (
                  <div> {errors.lastname}</div>
                )}
                <Form.Field>
                  <Checkbox label="I agree to the Terms and Conditions" />
                </Form.Field>
                <Button type="submit" onClick={handleSubmit}>
                  Submit
                </Button>
              </Form>
            );
          }}
        />
      </>
    );
    
    render(<App />, document.getElementById("root"));
    

    【讨论】:

    • TypeError: _this.props.onSubmit is not a function 提交时出错。
    • 我已经编辑了答案以在 @TheCoder 中包含 onSubmit 属性
    • @RahilAhmad 有什么办法可以将参数发送到 handlesubmit 以便稍后在 onsubmit 中收到?
    • @RishiSahu 您可以使该部分成为初始状态,这样您就可以从助手更新它并在内部提交时接收它,或者您可以手动调用带有任何其他值的 handleSumbit 想要的传给ti
    • @RahilAhmad 我确实尝试将参数传递给 handleSubmit 但我没有在 onSubmit 函数中收到这些参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2016-08-23
    • 2019-09-12
    • 2019-12-26
    • 2020-12-06
    相关资源
    最近更新 更多