bietkul / react-reactive-form

Angular like reactive forms in React.
MIT License
309 stars 32 forks source link

Store the form in state #6

Closed michaelspeed closed 6 years ago

michaelspeed commented 6 years ago

how can i store the form state in a db ?

bietkul commented 6 years ago

I think that you want to store the values of form in db, you can use the value property of control.

michaelspeed commented 6 years ago

but then i cannot revive it from there, is there a way to store the value with the controls?

bietkul commented 6 years ago

Why do you want to store the controls in your database, can you please specify your use case

michaelspeed commented 6 years ago

if i need to edit the form or view the form ?

bietkul commented 6 years ago

Create Mode : You just need to save the values in your db. Edit Mode: You can use the patchValue function to set the values of your form controls that's it.

Please refer the docs for better explanation


this.form.patchValue({
   name: "Something",
    gender: "Male"
 })
michaelspeed commented 6 years ago

patchValue does not work if there are arrays in the values.

bietkul commented 6 years ago

You have to do something like this.

const items = [
      {
        name: "item1",
        description: "desc 1"
      },
      {
        name: "item2",
        description: "desc 2"
      }
    ];
    items.forEach((item, index) => {
      this.addItem();
      this.productForm
        .get("items")
        .at(index)
        .patchValue(item);
    });

Check out https://codesandbox.io/s/nw9wxw2nvl

michaelspeed commented 6 years ago

thank will try it