admin管理员组文章数量:1404922
I have rendered a form item using an array. I want to write a custom validation to check whether the allocation fields sum up too 100 and not more or less than 100. How do I get the related allocation fields using 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>
I have rendered a form item using an array. I want to write a custom validation to check whether the allocation fields sum up too 100 and not more or less than 100. How do I get the related allocation fields using 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>
Share
Improve this question
edited Jul 1, 2022 at 0:23
pppery
3,82225 gold badges37 silver badges50 bronze badges
asked Apr 15, 2020 at 11:44
Clement OkyereClement Okyere
3213 gold badges6 silver badges12 bronze badges
1 Answer
Reset to default 3https://codesandbox.io/s/react-antd-form-array-fmp46?file=/index.js
I've just written the codesandbox for your problem
As you can see on the code, you can get the value by form.getFieldValue(['userBeneficiary',${index}
,'allocation'])
Update:
According to your request, I've added the validators. You can see the codesandbox
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')
)
本文标签: javascriptAnt design 4 validate form items from arrayStack Overflow
版权声明:本文标题:javascript - Ant design 4 validate form items from array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744859630a2628989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论