question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setFieldsValue not working while trying update onchange same form item

See original GitHub issue

setFieldsValue not working while trying update onchange same form item. (** If I am trying set value different form item its working )

not working

this.props.form.setFieldsValue({
                test: 100,
            });

working

this.props.form.setFieldsValue({
                otherFormItem: 100,
            });

Complete Code

@import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Form, Input } from 'antd';
const FormItem = Form.Item;

@connect()
class TestForm extends React.Component {

    handleChange = (e) => {
        console.log(e.target.value);
        console.log("before", this.props.form.getFieldsValue().test);

        if (e.target.value > 100) {
            this.props.form.setFieldsValue({
                test: 100,
            });
        }
        console.log("after", this.props.form.getFieldsValue().test);
    }
    render() {
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = 'vertical';
        return (
            <Form>
            	<br/>
                    <FormItem label="Test " {...formItemLayout}  >
                        {getFieldDecorator('test', {rules: [{ required: true, message: 'Please input your test !',  }],  onChange: this.handleChange, })(
                            <Input placeholder="test"   /> 
                             )}
                        </FormItem>
                       <FormItem label="otherFormItem " {...formItemLayout}  >
                        {getFieldDecorator('otherFormItem', {rules: [{ required: true, message: 'Please input your  !',  }],  onChange: this.handleChange, })(
                            <Input placeholder="otherFormItem"   /> 
                             )}
                        </FormItem>
                </Form>
        )
    }
}

const Test = Form.create({})(TestForm);

export default Test

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
luisgurmendezcommented, Sep 5, 2018

@daskyrk Here is how:

class ....{

onChange = (value) => {
// Custom method
//Example
if(value > 100){
   value = 100;
}

return value;
}



render(){
.
.

<FormItem>
      {getFieldDecorator("range-input", {
          getValueFromEvent: this.onChange,
        })(
       <NumericInput placeholder="%" />
       )}
</FormItem>
.
.

}
}
5reactions
daskyrkcommented, Apr 16, 2018

@iiitmahesh have you find out how to use getValueFromEvent ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Antd form setFieldValue on OnChange event is not working
I am trying to show the formatted value in the same text field. Please find the sandbox link here codeSandbox. I dont want...
Read more >
useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and have the options to validate and update the form...
Read more >
Form - Ant Design
Does not work when multiple Item with same name setting the initialValue. Why does onFieldsChange trigger three times on change when field sets...
Read more >
an antd bug of form setFieldsValue - CodePen
... is using the 'http' protocol, which may not work when the browser is using https. ... const {Form, Input, Select} = antd....
Read more >
form item checkbox antd | The search engine you control.
setFieldsValue if you cant't change using upper code. for functional component you have to take the form reference that is binding using useForm()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found