蚂蚁设计形式的验证器
我正在尝试编写一个表单。清单验证器以检查重复值(名称),如果我先在第一个索引处填写字段,则代码按预期工作,然后创建一个新索引(新的field行),然后填写字段在第二个索引,依此类推...但是如果我同时创建两个或更多行(索引),并尝试填写第二个索引(离开第一个空白),则会出现错误。请参阅我写的规则,
我整天都在尝试在名称字段上键入列表的值,以及是否有任何对象是null返回所需的错误消息,但没有运气。
有任何帮助与验证者打交道吗?
<Form.List name="ABC">
{(fields, { add, remove }) => (
<>
{fields.map(
({ key, name, fieldKey, ...restField }) => (
<Form.Item
label={`Series Name ${
fieldKey + 1
}`}
{...restField}
name={[name, "name"]}
fieldKey={[fieldKey, "name"]}
rules={[
{
required: true,
// whitespace: true,
message: "Missing series name",
},
({ getFieldValue }) => ({
validator(_, value) {
if (
!value ||
getFieldValue("ABC")
.filter(
(d) => d.name === value
).length === 1
) {
return Promise.resolve();
}
return Promise.reject(
new Error(
"Duplicate values are not allowed."
)
);
},
}),
]}
>
<Input placeholder="Series Name" />
</Form.Item>
</Col>
<Form.Item>
<MinusCircleOutlined
onClick={() => {
remove(name);
}}
/>
</Form.Item>
)
)}
<Form.Item {...button}>
<Button
type="primary"
onClick={() => {
add();
}}
block
>
Create Series
</Button>
</Form.Item>
</>
)}
</Form.List>
I'm trying write a Form.List validator to check for duplicate values (name), the code works as expected if I fill the field at the first index first then create a new index (new rows of field), then fill the fields at the second index, and so on... but if I create two or more rows(index) at the same time and try to fill the second index at first (leaving the first blank), it gives an error. See the rules I wrote
I have been trying this whole day to get the value of list when typing on the name field and if any object is null return desired error message but with no luck.
Any help to deal with validators?
<Form.List name="ABC">
{(fields, { add, remove }) => (
<>
{fields.map(
({ key, name, fieldKey, ...restField }) => (
<Form.Item
label={`Series Name ${
fieldKey + 1
}`}
{...restField}
name={[name, "name"]}
fieldKey={[fieldKey, "name"]}
rules={[
{
required: true,
// whitespace: true,
message: "Missing series name",
},
({ getFieldValue }) => ({
validator(_, value) {
if (
!value ||
getFieldValue("ABC")
.filter(
(d) => d.name === value
).length === 1
) {
return Promise.resolve();
}
return Promise.reject(
new Error(
"Duplicate values are not allowed."
)
);
},
}),
]}
>
<Input placeholder="Series Name" />
</Form.Item>
</Col>
<Form.Item>
<MinusCircleOutlined
onClick={() => {
remove(name);
}}
/>
</Form.Item>
)
)}
<Form.Item {...button}>
<Button
type="primary"
onClick={() => {
add();
}}
block
>
Create Series
</Button>
</Form.Item>
</>
)}
</Form.List>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您创建两个新行并填充第二行时,在过滤器中,您检查
d.name
===该值。由于您尚未在第一行或字段中输入任何值,因此name
当前在abc
数组中不确定。d.name
尚不存在,这就是为什么它给您错误的原因。如果您不触摸该领域,它将保持不确定。输入该字段后,它将添加
{name:somevalue}
解决方案:
只需在
d?.name ===值
中添加可选的链操作员?
。希望这能解决您的问题。
When you create two new rows and fill the second row, in filter you check for
d.name
=== to that value. Since you haven't enter any value in first row or field,name
is currently undefined inABC
array.d.name
doesn't exist yet that's why it gives you error.If you do not touch the field, it will remain undefined. Once you type into that field, it will add
{ name: somevalue }
Solution:
Just add Optional Chain Operator
?
ind?.name === value
.Hope this resolve your issue.
最后写了验证者,希望这对某人有帮助。(将改善这一点)
Finally wrote the validator, hope this helps someone.(will improve this)
我使用了此代码,有几个问题,例如显示错误最后索引,如果没有返回,则所有索引都会显示错误。
这是完整的修复和测试
I used this code there is several issue like it's show error last index only and if no return will all field show error with index.
here is full fix and tested