Fixes #3595. Replaces #4736. Credit to @SebastiaanKloos for the initial solution. Also fixes a longstanding issue where dynamically added fields were not considered when refreshing dependant fields.