I have a Redux form which is working as it should, except one thing: I need to create an array of checkboxes, so that the user can choose between multiple options.
In HTML/PHP one would write something like that:
<form>
…
<input type="checkbox" name="item[]" value="120" />
<input type="checkbox" name="item[]" value="231" />
…
</form>
So on Serverside one would receive an array like so (assuming each box is checked): $item = [120, 231], where each item in the array corresponds to the value of the checkbox.
Doing the same with a redux-form like so:
let items = [{name:…, value:…}, …];
<form>
{items.map(item => {
<Field component="input"
type="checkbox"
name={item.name + '[]'}
value={item.value}
})}
</form>
results in those inputs: <input type="checkbox" name="item[]" value="true" />, what is not what I would expect. Additionally, checking on checkbox, checks each of the array.
So I changed the name attribute of the <Field /> to
name={`item[${item.value}]`}
what makes the checkboxes work as expected, but in turn result in that data when submitted:
{
//index: 0, 1, ,…, 120, 121, … ,231, …
item: [undefinded,undefined,…,true,undefined,…,true,undefined,…]
}
So my Question is: Am I wrong with the creation of the checkboxes, especially their names, or do have to transform the data, once at initialization time and a second time on submit?
If I have to transform the data, where would be the best place for that?