New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
You cannot set field before registering it when set the value to dynamic form item #8880
Comments
Hello @kerrzhao. Please provide a online reproduction by forking this link https://u.ant.design/codesandbox-repro. Issues labeled by |
@yesmeck https://codesandbox.io/s/3rxj27q281 这里是代码演示。 预想效果是往动态框里设置默认值。 |
就像警告说的,你不能在 field 注册前去设置它的值,你在 componentDidMount 里设置 |
Hello @kerrzhao, we use GitHub issues to trace bugs or discuss plans of Ant Design. So, please don't ask usage questions here. You can try to ask questions on Stack Overflow or Segment Fault, then apply tag |
|
Same problem. English please. |
@naefl As warning stated, you can not call |
@naefl try 'setFields' instead |
@naefl We can use the |
Having this same issue: Occurs occasionally, but never the first time a component is mounted on React Native. What is strange about this, is that class Login extends React.Component {
componentDidMount() {
this.getStoredSettings()
}
getStoredSettings = async () => {
const canLogin = await getItem('token')
if (!canLogin) {
const creds = await getItem('credentials')
if (creds) {
// update values with creds
const { email, password } = JSON.parse(creds)
try {
this.props.form.setFieldsValue({
email,
password,
})
} catch (error) {
console.log(error)
}
}
}
}
render() {
const { form, initialValues } = this.props
const { email, password } = initialValues
const { getFieldProps } = form
return (
<Form>
<InputItem
{...getFieldProps('email', {
initialValue: email,
})}
/>
<InputItem
{...getFieldProps('password', {
initialValue: password,
})}
type='password'
/>
</Form>
)
}
}
const LoginForm = createForm()(Login)
const mapState = (state) => {
return {
initialValues: {
email: __DEV__ ? DEFAULT_EMAIL : '',
password: __DEV__ ? DEFAULT_PASSWORD : '',
},
}
}
export default connect(mapState)(LoginForm) |
@gitdust dose it work ? |
@fenghuizhang it's work for me |
why not like this ? |
if your form doesn't have the fields these you set, this error will appear! |
I having this same issue: |
多数是写错label了 |
if field not set, warning too. it works for me~ |
I'm having the same issue. I think it's related to the field value being an object like @kerrzhao said:
The field is set. if I do fields.hasOwnProperty(myField) it's returning true. |
i has the same issue,the reason is that,et a value to fields that form does not exist |
@guixuefeng101 the same with me |
The error should really say: "Registering" sounds like the problem is internal to the Ant framework, and has no meaning to new developers. |
@joeheyming You can send a PR to improve that instruction. |
@afc163 any pointers to where this message is? I can't find it |
use getFieldDecorator's initialValue instead setFields or setFieldsValue |
I have the similar issue, but thanks to @gitdust, your solution solved my problem perfectly. |
you can first set the form, like this {
_.isEmpty(gatherDetail) && this.props.location.state.type === 'edit' ? : null
}
|
表单组件里只有A和B,而你set的对象里多了一个C |
works for me , great! |
How to ignore this warning? |
@kerrzhao I'm trying to put
|
sixsixsix |
try |
you should definition your fields with
document can be more clearer in this kind of important message. |
awesome |
i had the same problem as my field name is set to a random value 'astermihagauvlent', i fixed it by changing the field name to 'keys' |
initialValue is not going to change after first render, just like defaultValue. Try setFieldValue or mapPropsToFields in this situation. example: |
you set a fields which you don't have. : ) |
真相了 |
@ShMcK it works for me, thanks a lot :) |
getFieldsValue 即可。 |
Make sure field registered before const { form } = this.props;
if (undefined !== form.getFieldValue('foo')) {
form.setFieldsValue({
foo: 'hello world',
});
} |
Thx |
For me, the issue was due to me calling setFieldsValue with an object that had several fields, while my form only had one field. Here is a function you can use to
Using that function, it will ignore any fields that do not exist in the form. |
you can't use resetFields reset it when use getFieldDecorator's initialValue |
This was the only solution found in the universe that worked useEffect hooks |
love you |
for me, i have multiple forms. The error i found is just caused by wrong form instance, that's why i pretty sure that the field has been registered, but cannot be set value via |
厉害呀,这个方法解决了我的问题,在useEffect hooks 中我想在分步表单中来收集值的问题 |
Version
3.1.1
Environment
MAC 、chrome 63.0.3239.84 (64)、antd 3.1.1
Reproduction link
https://ant.design/components/form-cn/#components-form-demo-dynamic-form-item
Steps to reproduce
Dynamic form item set value,
` const formItems = keys.map((k, index) => {
return (
<FormItem
{...formItemLayoutWithOutLabel}
required={true}
key={k}
>
{getFieldDecorator( minAmount-${k} , {
rules: [{
required: true,
message: "请输入大于0的值",
}],
})(
<InputNumber min={0.01} style={{ width: '10%', marginRight: 8 }} disabled={this.state.saveDisable} placeholder="大于0的值" step={0.01} />
)}
Example this.props.form.setFields( { "minAmount-1" : {value : 111} } );
.What is expected?
Set value as common fields.
What is actually happening?
You cannot set field before registering it.
When edit the row data and set the data to dynamic form item .
The text was updated successfully, but these errors were encountered: