As great as Rails validations are, they’ve left much to be desired for me. I find that I often want to only do partial validations, or use a different set of validations on my model based on who is creating it (admins should be trusted more than visitors) or where it is being created (creating a user on the command line or automated process shouldn’t require acceptance of terms or confirmation of a password).
I’m getting close to the point where I’m so annoyed about this problem that I’m ready to solve it, but I can’t quite figure out the best way to do it. So, I ask you, how do we remedy this?
Here are a couple ideas that I’ve had for changing how validations work.
My first thought is to just add it to the model by declaring “sets” of validations:
And then when you save a model, you can specify what sets to use.
I could also see this being used for a wizard like interface.
But that shouldn’t really be part of the model
You’re right, these problems really don’t concern the model. The model shouldn’t care were it is being created from, or if it’s being created in one step or five. So what about going to the opposite end of the spectrum, and completely removing validations from the model and declaring them in observer-like classes.
And then in the controller:
If we had an action that required different validations than the rest of the controller, we could also use similar approach to what we talked about earlier and declare what validations to use:
I like this better, because choosing what set of validations to use does seem more like a controller decision, and not model one, but it is the model’s responsibility to make sure that the data is “valid”, so how do we reconcile this?
How are you working around this problem? Is there an obvious solution that I’m just not seeing?