That's how I would do it, but I guess the more correct way to do it would to leave the transformer mounted on the outside of the box with the line side wiring on the inside like a doorbell transformer.
I've often thought that small transformers were probably less of a fire hazard inside the box than outside, except that the cover should be vented for heat dissipation. However, without a barrier your install is a violation, so no matter how the fire started you are on the hook; for that reason (liability) I wouldn't do that. If you relabeled the transformer "class 1" and used nm for the LV side, I'd say it was compliant. I like the class2 transformers that have built in short ckt and overload protection.
A forum community dedicated to professional electricians, contractors, and apprentices for residential and commercial work. Come join the discussion about trade knowledge, tools, certifications, wiring, builds, scales, reviews, accessories, classifieds, and more!