Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false positive "A form label must be associated with a control" #759

Closed
stardvst opened this issue Nov 25, 2020 · 11 comments
Closed

false positive "A form label must be associated with a control" #759

stardvst opened this issue Nov 25, 2020 · 11 comments

Comments

@stardvst
Copy link

I'm using airbnb eslint configuration, and eslint complains when input's label is wrapped in another element:

function App() {
  return (
    <form>
      <h2>
        <label htmlFor="new-todo-input">
          What needs to be done?
        </label>
      </h2>
      <input
        type="text"
        id="new-todo-input"
        name="text"
      />
    </form>
  );
}

I'm getting this error during compilation:

Failed to compile.
src\App.jsx
  Line 5:9:  A form label must be associated with a control  jsx-a11y/label-has-associated-control
@stardvst stardvst changed the title False positive "A form label must be associated with a control" false positive "A form label must be associated with a control" Nov 25, 2020
@manavm1990
Copy link

image

Me 2.

@ljharb
Copy link
Member

ljharb commented Nov 25, 2020

This isn't a false positive. The airbnb config requires that every input be BOTH for-id linked, and also the input nested inside the label.

@ljharb ljharb closed this as completed Nov 25, 2020
@manavm1990
Copy link

Thanks @ljharb I C this has come up in the past.

@kalm42
Copy link

kalm42 commented May 22, 2021

The error message needs to be updated. The form label is associated with a control. Please be more specific that "A label must be the parent of the associated control".

@ljharb
Copy link
Member

ljharb commented May 22, 2021

It’s not associated in every engine, though, unless it’s both nested and linked. A PR to improve the message would be welcome.

@liby
Copy link

liby commented Jun 7, 2022

This isn't a false positive. The airbnb config requires that every input be BOTH for-id linked, and also the input nested inside the label.

@ljharb A false positive occurred when working with Field of Formik:
image

@ljharb
Copy link
Member

ljharb commented Jun 7, 2022

@liby did you configure the plugin so it knows that a Field is an input?

@liby
Copy link

liby commented Jun 7, 2022

@liby did you configure the plugin so it knows that a Field is an input?

Thank you for reminding me. I haven't configured it yet because it works elsewhere. As shown below, I did not make any additional configure, nor did eslint-disable.
image

@ljharb
Copy link
Member

ljharb commented Jun 7, 2022

Then the warning is expected, because it’s not an input, it’s a Field. I’m not sure why it works elsewhere without that config.

@liby
Copy link

liby commented Jun 7, 2022

Then the warning is expected, because it’s not an input, it’s a Field. I’m not sure why it works elsewhere without that config.

Thanks for the tip. I configured it as follows, and it works.

"rules": {
  "jsx-a11y/label-has-associated-control": [
      "error",
      {
        "controlComponents": ["Field"],
        "assert": "both"
      }
  ],
}

But following the description in the readme doesn't work.
image

I guess it has something to do with the definition of Field.

@ljharb
Copy link
Member

ljharb commented Jun 7, 2022

@liby at this point i think a new issue would be warranted; but first make sure you're using the latest version of the plugin, and you're also looking at the docs not on main, but on the version tag you're using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants