Fixed #19617 and #8620 -- Refactored form metaclasses to support more inheritance scenarios.

Thanks apollo13 and mjtamlyn for the reviews.