Eslint-plugin-jsx-a11y: Form label must have associated control on compliant markup

Created on 24 Jul 2017  路  10Comments  路  Source: jsx-eslint/eslint-plugin-jsx-a11y

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

Most helpful comment

@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.

All 10 comments

Nevermind I needed to change the config from every to some

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

Could you elaborate on why it needs to be some?

@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.

@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?

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

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

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

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>

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?

@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.

@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

Was this page helpful?
0 / 5 - 0 ratings