How would I prevent this warping from occurring?
You wouldn't. Wood is a natural product and warping is what it naturally does. However, you can mitigate the effects. You can buy engineered wood products that won't warp, but I doubt that the cost would be acceptable for a project like this, and I don't know if engineered wood is available in pressure-treated for ground contact. (If it is, the price is, I'm sure, even higher.)
Did you confirm, before purchasing, that you were choosing the flattest, straightest, least warped boards in the pile at the big box? I've been known to set aside a dozen boards just to find one that I felt was true enough for my purposes. My wife and I can plan on an hour, minimum, at the lumber stack if we're picking up a large pile of wood for a project. (I've been known to put >50% into the "nope" pile. When it came to choosing the very visible decking for our recent deck build, it was probably >70% into the "nope" pile.) If the wood will be exposed, don't forget to check for knots, splits, checks and discoloration on the surface(s) that will be exposed. If the boards were warped, to begin with, it takes a lot more effort to ensure that your project will come out squareish.
Looking at your pictures, it looks like you've used untreated wood. That will not last very long in contact with dirt. It will begin to rot and fall apart almost immediately. You'll probably get 2 years where the top looks decent, but the part in contact with the ground will be rotting away. By year 3, you'll probably have visible rot visible near the top of the box. You'll want to purchase pressure-treated lumber that is rated for ground contact. At my preferred big box, they have "ground contact" and "not ground contact" rated PT. I'm not 100% certain the differences, but the cost factor is small, so it was worth it to me (especially for posts that support my deck, YMMV).
Dealing with crown:
To accommodate warped wood (all wood is going to have some warp to it unless you're buying kiln- or air-dried lumber from a quality lumber yard, i.e. not a big box, and even then, it can move on you when you cut it) you'll have to work with the wood instead of just assuming that everything's flat and square like a piece of steel would be.
If there is a bow in you 2x12 along its length, is it a slow, steady even bow? If so, you can probably find 2 reasonably matching sections of bowing.
- Cut your long lengths from these matching sections.
- Cut them a couple of inches longer than your then your finished length. (See the first point in the "Work with it" section. If you're going to use that method, add some to that measurement.)
- Stand them next to each other on their 2x edge on a level floor (like the garage).
- You'll want them to be crown up - they should be stable on the floor that way.
- If you put them crown down, they'll want to rock which will make the next step more difficult since they'll be moving.
- From there, mark a vertical line using a square to the floor.
- When you lay them down to recut them, you'll see that these new lines are not square to the wood, but they'll be vertical when these side rails are stood back up.
- Duplicate this process for the 2 short ends.
You'll now have 4 boards that have a vertical crown when stood on edge in the orientation you'll be assembling them, but most importantly they'll have edges that will match flat so they can be screwed together and meet up neatly. If this vertical crowning is unacceptable, you can use a straight-edge to mark a line that will cut off the crown. Cut this with your circular saw. You'll end up with a piece that's flat on the top and curved on the bottom - since the bottom will be buried in the dirt, it won't matter. In theory, the crown you cut off the top should, more-or-less, fit into the arch left on the bottom - you could glue and screw it in if the crown is significant.
Dealing with a twist:
If the boards have twist across their face (the 12" dimension) you have options. If you have a surface planer, you can plane the warp out, but then you end up with thinner boards that may not be suitable for the purpose. If the warp isn't too severe, you can simply work with it and forcibly pull the warp out as you assemble, or you can cut to accommodate the warp.
To pull the warp out:
- If it's not too severe, you may be able to simply push the board flush when you're screwing it in place, then add an extra screw or two to hold it there. Be warned, though, that the wood will want to move back, and you may end up with it pulling past the screw heads.
- If the warp is more severe, you may be able to use clamps to flatten the warp then use lag screws with washers (into predrilled holes) to hold the surface to the edge grain of the adjoining board. Again, the wood will want to move back and you may end up with splits as it does so.
Work with it:
To cut to accommodate the warp, scribe the joints:
- In this process, you'll be cutting roughly 1.5" off of each end of your long boards. Make sure you account for this when cutting the long boards by cutting them 3" longer than the desired finished length.
- Align the board to overlap (that would be the board we see end grain on in your picture #2, on the left) against the face of the board it's to overlap (the surface grain board in pic #2, on the right).
- Square the boards up the very best you can (ensuring that the joint remains square).
- Use a pencil to draw a line along the end-grain board onto the face-grain board.
- Use a jig-saw to cut the face-grain board along this line.
- When you bring them back together in their original alignment, they should mate up pretty accurately.
- You can tweak your jigsaw cut as necessary to meet your standards of tidiness. (Adjust the cut with the jig-saw, a rasp or file, or sandpaper as you feel necessary.)
- Remember that some of the joint (looks like) it will be buried, so that part probably doesn't have to be quite so neat (i.e. save some time here).
- Your corner on the face-grain board will no longer be perfectly vertical, but it will be a reasonably tight joint to the board around the corner from it and, unless there's a severe twist, it won't be obvious because you've got a tight joint. Additionally, some of this not-square-joint could be covered in dirt if you plan properly. (I keep mentioning that because it looks like you're building this into a slope and I'd imagine you'll back-fill to match the slope.)
I'd think that your deck screws will be sufficient for quite a number of years.
- Be sure you're using ones that specifically indicate that they're suitable for use in treated lumber (most decks are made of treated lumber, so that should be most deck screws).
- For any that will be directly exposed to dirt, you might want to consider the extra expense of stainless steel screws. SS screws are quite a bit more pricey, though, so you'll have to decide on your price vs life-span tolerance.
- You may want to increase the number of screws you're using. I'd guess you used 3 on a 12" run. You may want to up that to 4 screws.
- Consider pre-drilling pilot holes, even though many deck screws have drill-type tips.
- Using a small pilot hole bit will help reduce splitting when putting them in near the edge on either long- or edge-grain. Especially if you need to put more than one screw into a single growth ring.
- A small pilot hole bit should allow for a straighter hole for better alignment into the long length of the adjoining board.
Fixing This Box:
If you want to fix this box without cutting the ends to make it smaller, you could cut the warp off.
- Take the box to a hard, level surface.
- Remove the 2x4 top cap.
- Insert some shims under the high points to hold it steady (or enlist a helper - put the kids to work!).
- Use a level to find the lowest top corner.
- Draw a level line around the box from the lowest corner.
- Mark the 4 corners (near the bottom of the corner) to ensure you reassemble in the correct order.
- Disassemble the box.
- Cut the boards down at this new level line.
- Reassemble the box.
- For the corners that are slightly out of alignment (pic #2), I'd simply pull them into alignment on assembly and add an extra screw.
- Reattach the top cap (some fettling may be required to make it fit properly now).
The bottom of the box won't be square, but you can bury the low points a bit to hide the sins and call it good.
You may end up with some splits and checks, but since you're intentionally using non-PT lumber for your veggies, you'll be replacing the boxes every few years anyway, so it shouldn't be too big an issue.