简体   繁体   English

即使我在 react.js 中添加了验证,我也能够以 antd 步骤形式进入下一步

[英]I am able to move into next step in antd step form even though i have added validation in react.js

I am able to move into next step in antd step form even though i have added validation in react.js I have used default validation of antd form即使我在 react.js 中添加了验证,我也能够以 antd step 形式进入下一步,我使用了 antd 形式的默认验证

 const steps = [
    {
      title: "Service Address",
      content: (
        <Form form={form}>
          <Card>
            <h1>Lead Information</h1>
                <Form.Item
                  rules={[{ required: true }]}
                  name={"first_name"}
                  label="First Name"
                >
                  <Input />
                </Form.Item>
                       </Card>
                          </Form>
     },
    {
      title: "Service Information",
      content: "Second-content",
    },

And this my map loop to return the steps这是我的 map 循环返回步骤

return (
    <>
      <Steps
        current={current}
      >
        {steps.map((item) => (
          <Step key={item.title} title={item.title} />
        ))}
      </Steps>
      <div className="steps-content">{steps[current].content}</div>
        {current < steps.length - 1 && (
              <Button type="primary" onClick={() => next()}>
                Next
              </Button>
        )}
 )
</>

Can some one please help me with This!有人可以帮我解决这个问题吗! Thanks in Advance提前致谢

Looks like you are not doing it properly, validation only works if you submit a form, otherwise it doesn't trigger the validation.看起来您没有正确执行此操作,验证仅在您提交表单时才有效,否则不会触发验证。

You need to submit the form with the click of the Next button.您需要单击“下一步”按钮提交表单。

  const next = () => {
    setCurrent((prev) => prev + 1);
  };

  const onSubmit = (formValues) => {
    console.log(formValues)
    next();
  };
  const steps = [
    {
      title: "Service Address",
      content: (
        <Form form={form} onFinish={onSubmit}>
          <Card>
            <h1>Lead Information</h1>
            <Form.Item
              rules={[{ required: true }]}
              name={"first_name"}
              label="First Name"
            >
              <Input />
            </Form.Item>
          </Card>
        </Form>
      )
    },
    {
      title: "Service Information",
      content: "Second-content"
    }
  ];

  return (
    <div className="App">
      <Steps current={current}>
        {steps.map((item) => (
          <Steps.Step key={item.title} title={item.title} />
        ))}
      </Steps>
      <div className="steps-content">{steps[current].content}</div>
      {current < steps.length - 1 && (
        <Button type="primary" onClick={form.submit}>
          Next
        </Button>
      )}
    </div>
  );

You can see a working example here: https://codesandbox.io/s/upbeat-faraday-o16hj?file=/src/App.js您可以在这里看到一个工作示例: https://codesandbox.io/s/upbeat-faraday-o16hj?file=/src/App.js

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

相关问题 我怎样才能进入时间线的下一步 - how can i move to next step of timeline 我无法在 react.js 的标头中发送 JWT 令牌 - I am not able to send JWT token in headers in react.js 即使我设置了回调 function - React updating state for validation always a step behind even when i set a callback function 即使我使用React.js映射包含1,000个对象的数组,如何一次只向屏幕渲染25个对象 - How to only render 25 objects to the screen at a time even though I am mapping over an array containing 1,000 objects using React.js 如何单击“下一步”按钮第一步检查验证,第二步不检查验证? - How do I click Next button First step check Validation and second step Not check Validation? Bootstrap向导:如何从JavaScript进入下一步? - Bootstrap wizard: how can I move to the next step from JavaScript? 为什么我不能使用 react-router-dom 中的“路由”,即使它是版本 6? 它显示错误 - Why am I not able to use "Routes" from react-router-dom even though it is version 6? It shows error 我无法在 React.js 中更改我的 state 对象的 state - I am not being able to change the state of my state objects in React.js Angular JS和验证逐步 - Angular JS and validation step by step 当我使用 react.js 传递到 mysql db 时,我无法更改日期格式 - i have a problem to change the format of date when i am passing to mysql db using react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM