0

Im developing a Soccer Betting system. I want to make a form dynamically and keep the objects in the state with redux-forms.

My basic entity is a Match: Who has a home_team, a away_team, and inputs with a home_result and a away_result.

 matches = [
{
  name: 1,
  type: "group",
  home_team: 1,
  away_team: 2,
  home_result: null,
  away_result: null,
  date: "2018-06-14T18:00:00+03:00",
  stadium: 1,
  channels: [],
  finished: false
},
{
  name: 2,
  type: "group",
  home_team: 3,
  away_team: 4,
  home_result: null,
  away_result: null,
  date: "2018-06-15T17:00:00+05:00",
  stadium: 12,
  channels: [],
  finished: false
},

I want to fill it in a Redux-Form, but get stuck in the best way to make it. I want too that, every time a user changes the values on the input, this is reflected in the State-Json.

4
  • just to see if I got it right, every match is a new redux-form with inputs of the result? Commented Mar 21, 2018 at 9:28
  • This is the problem. Its a good pattern to have a lot of forms on the same page? I was thinking that every match is a part of a big form (every match has your own id). Got it ? Commented Mar 21, 2018 at 11:28
  • How many matches do you show per page? If you want to dynamically create the form by the matches you can pass the matches as an initialValue and map through the array to create Fields in the form. got it or should I post a code as an answer? Commented Mar 21, 2018 at 11:46
  • I want to create it dynamically Matan. Thats where I get stuck. If you can show some code with redux-form I will aprecciate. Commented Mar 21, 2018 at 11:51

1 Answer 1

1

To dynamically create a form, you will need to build your data differently. You need fields object that will look like this(with the matches id):

const fieldsObject = ['match1', 'match2', 'match3']

And a initialValues object that will look like this:

const resultsObject = {
  match1_home: 1, 
  match1_away: 3
}

And so on. Then, the Form will initial its fields based on the initialValues and the names of the fields. The code:

const MyForm = (props) => {
 const { handleSubmit, fields } = props;
return (
    <form onSubmit={handleSubmit}>
      {fields.map(match => (
        <div key={match}>
          <Field
            name={`${match}_home`}
            component="input"
            />
           <Field
            name={`${match}_away`}
            component="input"
            />
        </div>
      ))}
      <button type="submit">Submit</button>
    </form>
  )
}

And the usage will look like this:

<MyForm initialValues={resultsObject} fields={fieldsObject} onSubmit={this.submitForm}/>
Sign up to request clarification or add additional context in comments.

2 Comments

It looks nice Matan, but I want that in the state the objects its a json: {match: id:1{ "home_score":1 , "away_score" ; 2} for example. Im being clear? Thanks for the answer
you can build the objects from the json with a map function over each key in your json... Have I missed something?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.