This makes it especially hard – if you don’t know what something’s called, how do you find more documentation on it? I am trying to re-architect a web application I developed to use the MVC pattern, but I'm not sure if validation should be handled in the model or not.
The only mention of it I could find in the API docs was here.
And the API docs for these validations don’t mention custom contexts at all! Let’s see what it looks like for our example: can’t take a validation context as a parameter. ) Custom validation contexts are useful for more than just saving a record in different ways.
This separation makes it possible to build reusable models, controllers and forms that can be loosely coupled through dependency injection.
Think of input validation as whitelist validation (“accept known good”) and model validation as blacklist validation (“reject known bad”).
That is, the controller must translate user data (which most of the time is just strings) into something meaningful.
This requires parsing (and may depend on such things as the locale, given that for example, there are different decimal operators etc.).The above code works for built-in functions like There's a couple uses I'd like to use it for.I'm writing an A* class which computes the shortest path on a node-edge graph, but I'd like to give the user the ability to customize the heuristic function instead of just using the default euclidean distance to the goal.However, when the date is then passed to the model, the model must maintain integrity. You want your business rules to always be enforced, no matter what happens.For example, it might make sense to not allow dates in the past, or dates, that are on holidays/sundays, etc. Assuming you had business rules in the controller, then you'd have to duplicate them, should you ever create a different controller.But you wouldn’t have all the nice form handling you get from Rails.