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

Form label must have associated control on compliant markup #302

Closed
sppatel opened this issue Jul 23, 2017 · 11 comments
Closed

Form label must have associated control on compliant markup #302

sppatel opened this issue Jul 23, 2017 · 11 comments

Comments

@sppatel
Copy link

sppatel commented Jul 23, 2017

This example markup is compliant.

<label htmlFor="firstName">First Name</label>
<input id="firstName" type="text" />

Why is linting failing with..

error  Form label must have associated control  jsx-a11y/label-has-for
@sppatel
Copy link
Author

sppatel commented Jul 23, 2017

Nevermind I needed to change the config from every to some

"required": {
                "some": [ "nesting", "id" ]
            }

@sppatel sppatel closed this as completed Jul 23, 2017
@captDaylight
Copy link
Contributor

Could you elaborate on why it needs to be some?

@ljharb
Copy link
Member

ljharb commented Dec 14, 2017

@captDaylight the default is "every" - you should both for-id-link and label-wrap your inputs. "some" means it will pass if you do either one of those.

@captDaylight
Copy link
Contributor

@ljharb hey thanks for the quick response, though I don't follow. I have a label and and input as at the top of the issue, htmlFor in label and id in input and it's throwing this error. What do you mean by for-id-link and label-wrap?

@ljharb
Copy link
Member

ljharb commented Dec 14, 2017

@captDaylight i mean that a) the label's "for" matches the input's "id", and also b) the input must be inside the label.

@captDaylight
Copy link
Contributor

captDaylight commented Dec 14, 2017

GOTCHA, ok, that makes sense. Thanks for the time!

@mdrideout
Copy link

mdrideout commented Jan 26, 2018

Just to help others that may find there way here, I simply did not realize that for my babel and jsx-a11y to work correctly together, I needed to use <label htmlFor... rather than just <label for...

The following format passed all the linter rules. This did not require me to make the adjustment to my config / eslintrc rules.
<label htmlFor="email">Email<input id="email" name="email" type="email" placeholder="Email Address" /></label>

@skirankumar7
Copy link

Nevermind I needed to change the config from every to some

"required": {
                "some": [ "nesting", "id" ]
            }

Could you please tell me what is config, where it is to change every to some?

@Christopotamus
Copy link

@skirankumar7
I ran into this issue as well, here's what my eslintrc.json looks like after I got it working:

{
    "extends": "airbnb",
    "env": {
      "browser": true,
      "jest": true
    },
    "rules": {
      "jsx-a11y/label-has-associated-control": [ "error", {
        "required": {
          "some": [ "nesting", "id"  ]
        }
      }],
      "jsx-a11y/label-has-for": [ "error", {
        "required": {
          "some": [ "nesting", "id"  ]
        }
      }]
    }
}

As you can see, I had to add the new stuff under "Rules" for the two rules that I was getting errors from.

fungjj92 referenced this issue in project-icp/bee-pollinator-app Nov 8, 2018
Quiet an uncommon eslint rule about <form /> format that complained after being satisfied. See: evcohen/eslint-plugin-jsx-a11y#302

Refs #316
fungjj92 referenced this issue in project-icp/bee-pollinator-app Nov 8, 2018
Quiet an uncommon eslint rule about <form /> format that complained after being satisfied. See: evcohen/eslint-plugin-jsx-a11y#302

Refs #316
fungjj92 referenced this issue in project-icp/bee-pollinator-app Nov 9, 2018
Quiet an uncommon eslint rule about <form /> format that complained after being satisfied. See: evcohen/eslint-plugin-jsx-a11y#302

Refs #316
@NishargShah
Copy link

@skirankumar7
I ran into this issue as well, here's what my eslintrc.json looks like after I got it working:

{
    "extends": "airbnb",
    "env": {
      "browser": true,
      "jest": true
    },
    "rules": {
      "jsx-a11y/label-has-associated-control": [ "error", {
        "required": {
          "some": [ "nesting", "id"  ]
        }
      }],
      "jsx-a11y/label-has-for": [ "error", {
        "required": {
          "some": [ "nesting", "id"  ]
        }
      }]
    }
}

As you can see, I had to add the new stuff under "Rules" for the two rules that I was getting errors from.

Restart your local server and its works fine

juhoeerola added a commit to Vincit/VISDOM-Roadmapper that referenced this issue May 24, 2021
Eslint does not recognise linking label with control when the control is
outside the label element. Fix found here:
jsx-eslint/eslint-plugin-jsx-a11y#302 (comment)
AleksiKytola pushed a commit to Vincit/VISDOM-Roadmapper that referenced this issue May 24, 2021
Eslint does not recognise linking label with control when the control is
outside the label element. Fix found here:
jsx-eslint/eslint-plugin-jsx-a11y#302 (comment)
eh-am added a commit to eh-am/pyroscope that referenced this issue Aug 5, 2021
as per jsx-eslint/eslint-plugin-jsx-a11y#302
requiring a label to be both nested and having a forHtml
is unnecessary
@jeojoe
Copy link

jeojoe commented Nov 1, 2021

label-has-for was deprecated in eslint-plugin-jsx-a11y v6.1.0 in favor of label-has-associated-control rule and required.some rule config has changed to assert.

Available values for assert config are 'htmlFor', 'nesting', 'both', 'either'

{
  // ...
  rules: {
    'jsx-a11y/label-has-associated-control': [
      2,
      {
        assert: 'either', // either check for `htmlFor` or `nesting`
      },
    ],
  }
}

yuki-yamamura added a commit to yuki-yamamura/udemy-testing-library that referenced this issue Apr 26, 2023
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

8 participants