question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to populate array with checkboxes?

See original GitHub issue

How to populate array with checkboxes?

Hi, I’m wondering if it’s possible to do this. I have 2 check-boxes, and every time one is checked, an array should be populated. I’ve been playing around, even tried the latest FieldArray, but I’m not sure I understand well how to use it, or even if it’s for that purpose.

Info

I am using Formik component with render method. Something like:

import React from 'react';
import { render } from 'react-dom';
import { Formik } from 'formik';


const App = () => (
  <div>
    <h1>My Form</h1>
    <Formik
      initialValues={{
        groups: [],
      }}
      onSubmit={values =>
        console.log(values)
        // Expected output would be something like:
        // {groups: ['admin', 'normal']}
      }
      render={props => (
        <form onSubmit={props.handleSubmit}>
          <input
            type="checkbox"
            name="groups.0"
            value="admin"
            onChange={props.handleChange}
            onBlur={props.handleBlur}
            checked={props.values.groups === 'admin'}
          />
          Admin
          <br/>
          <input
            type="checkbox"
            name="groups.1"
            value="normal"
            onChange={props.handleChange}
            onBlur={props.handleBlur}
            checked={props.values.groups === 'normal'}
          />
          Normal
          <br />

          <button type="submit" >
            Submit
          </button>
        </form>
      )}
    />
  </div>
);

render(<App />, document.getElementById('root'));

Here is an example: https://codesandbox.io/s/03pmmoo0kp

But what I really want is to have this as a result:

{
  groups: ['admin', 'normal']
}

Is it possible doing something like that? What am I missing?

  • Formik Version: 0.11-beta
  • OS: Debian unstable
  • Node Version: node@v8.6.0
  • Package Manager and version: yarn@1.3.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

48reactions
eladocommented, Mar 30, 2018

Example for a checkbox array using FieldArray: https://codesandbox.io/s/o5pw1vx916

11reactions
adamduncancommented, Feb 5, 2018

@jaredpalmer - now v0.11 is released and FieldArray’s stable, is it possible to add examples of creating fieldsets of multiple radio and checkbox inputs please? Seems such a common feature of forms, but yet to see a definitive example in the docs. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

html - create and populate with DOM a checkbox list with array ...
Here's one way (pure JavaScript, no jQuery): var animals = ["lion", "tigers", "bears", "squirrels"]; var myDiv = document.
Read more >
How to get all selected checkboxes in an array using jQuery
Given a number of checkboxes. The task is to get the value of all selected checkboxes in the array using JQuery.
Read more >
Checkbox memory problem (was: returning an array) - Jazz.net
So after realize() the string array storage can perish? I think I can work with that. The only reason I was using stringhttp://]...
Read more >
Get multiple selected (checked) CheckBox values in Array ...
When the Get Button is clicked, the CheckBoxes will be referenced using a loop. Inside the loop if the CheckBox is selected (checked)...
Read more >
JavaScript Array of checkboxes - YouTube
JavaScript Array of checkboxes. Watch later. Share. Copy link. Info. Shopping. Tap to unmute. If playback doesn't begin shortly, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found