Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update antd form if initialValue is changed

Tags:

reactjs

antd

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.

like image 255
Jide Avatar asked Apr 25 '20 07:04

Jide


People also ask

Why did my antd form item validation fail?

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

How to update initialvalue of form input fields?

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!

Why is the initial value of an un-operated field not changing?

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.

How to add form controls automatically in antd?

It is set in ANTD.nameAttributeForm.ItemPackaging control, form controls will be added automaticallyvalue (or valuePropNameSpecified other attributes)onChange (or triggerThe other attributes specified),...


1 Answers

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

like image 199
Julian Salamanca Avatar answered Oct 10 '22 23:10

Julian Salamanca