cerebral-legacy / cerebral-module-forms

Form handling for Cerebral
http://cerebral-website.herokuapp.com/documentation/cerebral-module-forms
MIT License
12 stars 5 forks source link

How best to do update form #30

Closed colinf closed 8 years ago

colinf commented 8 years ago

None of the demo projects seems to cover the use case of a form used for updating data, in all cases the 'value' of each field is set when the form is added to state. To make it work I am using code along the following lines in an action when the update form (teamInputForm) is opened:

  const {teams, selectedId} = state.get();
  const {id, name} = teams[selectedId];
  state.set(['teamInputForm', 'id', 'value'], id);
  state.set(['teamInputForm', 'name', 'value'], name);

Is this the best way to approach this? I'm happy to contribute an update form example to the demos if anyone can confirm the best approach.

Thanks, Colin

colinf commented 8 years ago

One day on & I think my previous post showed some Cerebral naïvety! I now think it's better to instantiate the form and set the state with it once the values are known. So in the same action this is now:

  const {teams, selectedId} = state.get();
  const team = teams[selectedId];
  state.set(['teamInputForm'], makeForm(team));

May well still be naïve but it seems to work!

edgesoft commented 8 years ago

@colinf Sorry for not answering, have been on holiday 👍 . When instantiating the form you should do something like

teamInputForm: Form({
    name: {
      value: '' // initial state
    },
   id: {
     value: '' // initial state
   }
})

You can also use defaultValue for this. Or maybe I understood it wrong?

colinf commented 8 years ago

I hope you had a good holiday! Thanks for the response - I think I got there in the end.