I have a 2x4 wall which doesn't allow for much room (maybe 1/4") behind the diverter valve. I do not want to extend the wall outward into the shower as that would be a headache. How can I secure the diverter valve (and pipes and everything else) into place so it is secure?
I have to wonder if your next problem will be that the valves are too far back in the wall to install the handles when the shower walls are installed. My experience with shower valves (not, I admit, a huge number of them) is that they normally don't sit all the way back in a 2x4 wall cavity when at the correct depth, so there's normally room for a 3/4" board behind them to screw into, at least.
To directly address the mounting issue, assuming they are where they belong, depth-wise: You can either replace a strip of drywall behind it with a board (or plywood) so you have some wood to screw into, or use a steel bracket and machine screws - either one mounts to the studs.