It happens sometimes that you have to implement creation or editing of a model splitted on more than one form.
A classic case is a registration form divided into two steps or a guided wizard with several pages.
This situation presents us with a problem: when and how to validate data?
The first idea that may come to mind is simply to leave it all validations in the last page when you go to create or update the model. But this is also the least elegant solutions, both in terms of code than usability.
What to do?
I present a technique that I adopted in similar circumstances to achieve “partial validation of a model”.
The idea is to implement a method that validates only the attributes present at every step.
We begin by defining, within our model, a method to validate a single attribute:
def self.valid_attribute?(attrib, value)
This method uses of a mock object to simulate the validation of model and after it verifies whether the hash of validation errors contains the attribute (attrib) that interests us.
If not, the method simply return true, regardless of other errors, thus overcoming the validation (partial!) of the model.
But let’s see an example.
Suppose we want to create a registration form in two steps, where in the first step login and password are required and in the second one anagraphic data are required.
We also add the constraint that registration is to be considered complete only when all data are provided.
In such a situation it is convenient to use partial validation.
We go to define the user model as follows:
class User < ActiveRecord::Base
Now suppose we have a first form that requires users to enter login, password and email and a second form that asks for personal information.
The two forms call respectively the methods step1 and step2 of the UserController.
In the step1 we can now use the valid_attribute? method defined above to validate only the attributes that interest us.
class UserController < ApplicationController
In the step2 we execute instead the save method the runs the “classic” Rails validation, cause it is the last step of our registration wizard and is here where you actually creates the user.
With the technique shown you can add as many steps you want, with the care of not ever save the model until it has reached the last step.
If you need to make more wizards inside your application, may be convenient to move the validate_attribute? method in a module to be used as mixin classes where you need it.
Obviously this is only one possible solution to the partial validation problem.
Have you ever faced the partial validation problem?
And if so, how did you solved it?