I need to create a new field (simple _light switch_), and assign it to a Category.
Everything works fine, until I enter a parent (level 0) category from the admin and I get this:

What I have:
lightswitch): OKInvalid field handle: (with the handle name I've set)Thank you for you help.
Hm, strange. Do you have any other fields with a similar handle (perhaps with different casing)?
Can you try deleting your storage/runtime/compiled_classes folder and see if that helps?
I've already tried changing to other names, but I've tried again with the name testingSomething: same result.
I've now deleted the folder: same result.
Now I see this, in the $fieldHandles array note the testingSomething:
...
'subTitle' => true,
'subTitle1' => true,
'subTitle2' => true,
'tags' => true,
'template' => true,
'termsConditions' => true,
'testingSomething' => true,
'textColor' => true,
'title1' => true,
...
Any additional hint is more than welcome. 馃憢
I鈥檓 not really sure what else to check off the top of my head. Any chance you can zip up your site and send it to [email protected], along with a database backup?
I'm actually experiencing this same issue. Only way I could solve it was to delete the category from the system and create it new with the fields required. This is happening on a normal PHP composer install on a staging server.
I can't speak to nested categories though as ours are only a single level.

Turns out this was due to different FieldLayout IDs in the DB.
Can't really say if it was a Craft's issue or our own environment problem. Sorry...
This is still an issue for me. I don't know if it's FieldLayout IDs mismatching, but once it happens, I still have to recreate the entire category.
@lxcodes Next time it happens, please zip up your site and send it to us at [email protected], along with a database backup.
I have the same issue, when I added the field to the section on the local env worked fine, but when I did the same on production got the same error.
@ricricucit what do you mean by a different FieldLayout IDs, please ?
You can have different "field layouts" defining how fields are displayed in the Control Panel, when editing the Category (or any other entry type). These different representations are stored in the database and each of these representations have their own FieldLayout ID.
For some reason, I've noticed that the "FieldLayout ID" that was assigned to those "broken Categories", was different from the FieldLayout ID that other "working categories" had.
Manually setting the same FieldLayout ID of "working categories" to the FieldLayout ID of the "broken categories", solved my problem.
Clear, thanks a lot for clarify that.
I was having the exact same problem, the solution of @ricricucit works:
Still curious how this happens.. The incorrect field layout contains fields which should belong to an entry instead of my category. I am wondering where this gets mixed up. Removing all fields and tabs from the category and re-adding the fields does not fix this either.
I am using the latest version of Craft (Craft Pro 3.0.36) with PHP 7.2.11. The problem exists on multiple Docker-environments as well as our staging & production environments.
Most helpful comment
You can have different "field layouts" defining how fields are displayed in the Control Panel, when editing the Category (or any other entry type). These different representations are stored in the database and each of these representations have their own FieldLayout ID.
For some reason, I've noticed that the "FieldLayout ID" that was assigned to those "broken Categories", was different from the FieldLayout ID that other "working categories" had.
Manually setting the same FieldLayout ID of "working categories" to the FieldLayout ID of the "broken categories", solved my problem.