[英]Ant-Design form validate returns old values while using onChange
I am using ant-design Form Item like this: 我正在使用像这样的ant-design Form Item:
{getFieldDecorator('city', {initialValue:'a', rules: [{required:true,message:' '}]})(
<select onChange={onChange} className="form-control">
<option value='a' >a</option>
<option value='b' >b</option>
</select>
}
and here is my onchange function:
onChange=()=>{
form.validateFields((err, values) => {
console.log(values)
})
}
The problem is that while changing value to 'b', I still get 'a' as the form value in onChange function (console.log(values)). 问题是,在将值更改为'b'时,我仍然将'a'作为onChange函数中的表单值(console.log(values))。 This happens while changing value from 'b' to 'a' too.
这也是在将值从'b'更改为'a'时发生的。
You are mixing up immediate onChange behaviour with validation. 您正在混合立即onChange行为与验证。 If you are looking for a dropdown with immediate action you do not need a
<Form>
, just use the <Select>
by itself. 如果您正在寻找立即行动的下拉列表,则不需要
<Form>
,只需单独使用<Select>
。
If you want a proper form with a submit function, you should not use onChange
on the <Select>
at all, that is handled automatically by antd. 如果你想用一个提交功能的适当的形式,你不应该使用
onChange
在<Select>
所有,即自动antd处理。 And then you will have proper values when you call form.validateFields
at form submit time. 然后,当您在表单提交时调用
form.validateFields
时,您将拥有正确的值。
Finally, if you do want immediate actions using field validation inside a <Form>
, use the onFieldsChange
option rather than individual widget onChange
. 最后,如果您确实希望在
<Form>
使用字段验证立即执行操作,请使用onFieldsChange
选项而不是单个窗口小部件onChange
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.