I'm passing currentUser data from my Redux-saga into antd form, Name, email, phone number, introduction are passed to the form as initialvalue, what i want to do is i want to submit the form as a put request to db if the initialvalue has been changed.. here is my code
import React from 'react';
import {createStructuredSelector} from 'reselect';
import { connect } from 'react-redux';
import { selectCurrentUser } from '../../redux/user/user.selector';
import { Form, Input, Button } from 'antd';
import './profile_form.styles.css'
/* eslint-disable no-template-curly-in-string */
const ProfileForm = ({currentUser}) => {
const {name, email} = currentUser
const onFinish = values => {
console.log(values);
};
const layout = {
labelCol: {
span: 7,
},
wrapperCol: {
span: 15,
},
};
return (
<Form className='profile-form' {...layout}
onFinish={onFinish}
initialValues={{ firstname: name, lastname: name, email: email }}
>
<Form.Item
name='firstname'
label="First Name"
>
<Input />
</Form.Item>
<Form.Item
name= 'lastname'
label="Last Name"
>
<Input value={name}/>
</Form.Item>
<Form.Item
name='email'
label="Email"
>
<Input value={email} />
</Form.Item>
<Form.Item name= 'phonenumber' label="Phone Number"
>
<Input />
</Form.Item>
<Form.Item name='introduction' label="Introduction"
>
<Input.TextArea />
</Form.Item>
<Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 8 }}>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};
const mapStateToProps = createStructuredSelector({
currentUser:selectCurrentUser
})
export default connect(mapStateToProps) (ProfileForm);
i found "onValuesChange" in antd api but have no idea how it's to be used.
1 antd form item validation fails if default value haven't been changed 0 Two Antd forms, one component Hot Network Questions Bypassing trademarks by changing words if a name is a combination of multiple words
To update initialValue form input fields need to be cleaned or reset. So add form.resetFields () (to reset data in input fields) after successful submission of form. Thanks for contributing an answer to Stack Overflow!
It is because your initialValue has no value when the form is initializing. In v3, modifying the initialValue of un-operated field will update the field value synchronously, which is a bug. However, since it has been used as a feature for a long time, we have not fixed it.
It is set in ANTD.nameAttributeForm.ItemPackaging control, form controls will be added automaticallyvalue (or valuePropNameSpecified other attributes)onChange (or triggerThe other attributes specified),...
If you are using hooks you can add an useEffect with the code:
useEffect(() => {
form.setFieldsValue(defaultValues)
}, [form, defaultValues])
Remember define the form value first:
const [form] = Form.useForm()
And remember to pass this form instance to the Form component:
<Form
form={form}
layout="vertical"
name="form-name"
initialValues={defaultValues}
onFinish={submitHandler}
>
In this form, you going to update the defaultValues when the component renders. If you are using classes, you can put the code inside componentDidUpdate lifecycle method.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With