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.

Test for empty items in radios and checkboxes

See original GitHub issue

I’d like to propose testing if items inside the items array for checkboxes and radios are empty. If they are, don’t output markup.

My use case

I want to conditionally show a radio based on some data.

In Nunjucks I can alter a value of an item in an array by adding a conditional after it - but it still outputs an empty item and I’ve not worked out how to not provide it.

I could prepare my data in advance - but for a simple thing like this, doing it directly is more readable IMHO. You can see the logic directly.

Example macro:

{{ govukRadios({
  idPrefix: "where-do-you-live",
  name: "where-do-you-live",
  fieldset: {
    legend: {
      text: "Where do you live?",
      isPageHeading: true,
      classes: "govuk-fieldset__legend--xl"
    }
  },
  items: [
    {
      value: "england",
      text: "England"
    },
    {
      value: "scotland",
      text: "Scotland"
    },
    {
      value: "wales",
      text: "Wales"
    },
    {
      value: "northern-ireland",
      text: "Northern Ireland"
    },
    {
      divider: "or"
    } if data.showFantasy == true,
    {
      value: "narnia",
      text: "Narnia"
    } if data.showFantasy == true
  ]
}) }}

This mostly works - but right now outputs a blank space and random html doing nothing: Screenshot 2019-07-12 at 15 23 36 Screenshot 2019-07-12 at 15 24 12

Suggested fix

Either strip out empty items from the items array or else test whether they’re empty when iterating over them.

This feels like a safer thing to do / more defensive. We don’t output markup where there’s no input.

I had a quick test of this by editing the radios macro directly in my node_modules, and it appears to work well.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
NickColleycommented, Jul 17, 2019

I’ve had a go at this and it looks like a sensible suggestion to only output items when they are truthy like Ed has suggested.

Will see what the rest of the team thinks, we’d want to do this for all the components I’ve mentioned above as well.

0reactions
36degreescommented, Aug 30, 2019

Ah, sorry @edwardhorsford, my bad. If you can raise a separate issue that’d be great – I don’t believe it’s solved by #1512 (though we do test for {% if item.conditional %}, so I’m not entirely sure what’s going on there).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I check if the returned value of a radio button is ...
You're grabbing the elements that are checked. · I wanted to use return and was wondering if there is a way to check...
Read more >
UI cheat sheet: radio buttons, checkboxes, and other ...
When you select an item in a radio button list, you can't deselect it. You can select something else and change your selection,...
Read more >
Checkboxes vs. Radio Buttons
Because radio buttons require exactly one choice, make sure that the options are both comprehensive and clearly distinct. In tests with older ...
Read more >
Include all fields – checkboxes/radio buttons, empty or not
I am finding the plugin really helpful but have a question regarding one of my forms. One of my forms (a 'trip request...
Read more >
How to check a radio button using JavaScript
The input radio checked property is used to check whether the checkbox is selected or not. Use document.getElementById('id').checked method for this. It will ......
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