【问题标题】:Ant design 4 validate form items from arrayAnt design 4 验证数组中的表单项
【发布时间】:2020-04-15 11:44:03
【问题描述】:

我是 ant 设计的新手,我使用数组渲染了一个表单项。我想编写一个自定义验证来检查分配字段总和是否超过 100 且不大于或小于 100。如何使用 getFieldValue 获取相关分配字段?

 <Form.Item
              label="Allocation "
              name={["userBeneficiary", `${index}`, "allocation"]}
              rules={[
                ({ getFieldValue }) => ({
                  validator(_, value) {
                    console.log(
                      "fields value from allocation",
                      getFieldValue("allocation")
                    );
                    if (!value && getFieldValue("allocation") === "") {
                      return Promise.reject("please input allocation!");
                    }
                    return Promise.resolve();
                  },
                }),
              ]}
            >
              <Input disabled={uploadState.upload.formDisabled} />
     </Form.Item>

【问题讨论】:

    标签: antd


    【解决方案1】:

    https://codesandbox.io/s/react-antd-form-array-fmp46?file=/index.js

    我刚刚为您的问题编写了代码框 正如你在代码中看到的,你可以通过 form.getFieldValue(['userBeneficiary',${index},'allocation']) 获取值

    更新:

    根据您的要求,我已添加验证器。可以看到codeandbox

    import React from 'react'
    import ReactDOM from 'react-dom'
    import { Form, Button, InputNumber } from 'antd'
    import 'antd/dist/antd.css'
    import './index.css'
    
    const MyForm = () => {
      const mockdata = ['a', 'b', 'c']
      const [form] = Form.useForm()
    
      return (
        <Form form={form}>
          Hello
          {mockdata.map((item, index) => (
            <Form.Item
              label="Allocation "
              name={['userBeneficiary', `${index}`, 'allocation']}
              rules={[
                {
                  required: true,
                  message: 'This field is required!'
                },
                {
                  type: 'number',
                  message: 'Please input number!'
                },
                ({ getFieldValue }) => ({
                  validator(rule, value) {
                    if (index < mockdata.length - 1) {
                      return Promise.resolve()
                    }
                    let sum = 0
                    for (let i in mockdata) {
                      sum += parseInt(
                        getFieldValue(['userBeneficiary', i, 'allocation']),
                        10
                      )
                    }
                    if (sum >= 100) {
                      return Promise.reject('Sum should be less than 100!')
                    }
                    return Promise.resolve()
                  }
                })
              ]}
            >
              <InputNumber min={0} max={1000} />
            </Form.Item>
          ))}
          <Button type="primary" htmlType="submit">
            Submit
          </Button>
        </Form>
      )
    }
    
    ReactDOM.render(
      <div className="App">
        <MyForm />
      </div>,
      document.getElementById('root')
    )
    
    
    

    【讨论】:

    • 如果您能告诉我如何验证所有分配字段的总和是否为 100,将不胜感激
    • 我已经添加了验证器
    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2019-12-25
    • 2019-10-04
    • 2019-06-17
    • 2020-11-30
    • 2018-12-19
    • 2021-07-11
    • 2017-06-23
    相关资源
    最近更新 更多