Topic: Running validations without @errors
I'm refactoring a bit of code in my application that handles data integrity. Basically, I have methods like user.step1_completed? and user.step2_completed? that are just conditional statements returning true or false. Since I already have my validations set up, I want to DRY up the logic by simply running the validations and returning true or false (like user.valid?). For example, this is what I've done (which works with the exception of nested attributes) Note that I'm using a custom validator that I made which just specifies which validations to run:
def step1_completed? mock = self.dup mock.validated_fields = [ :first_name, :middle_name, :last_name, :email, :street_address, :city, :state_str, :zip_code, :birth_date, :currently_insured ] validity = mock.valid? self.valid? if original_errors_count > 0 #because self.errors gets cleared when running mock.valid? return validity end
The problem is context -- I use these methods in my views, and doing so can alter the @errors, which can really screw up the error messages shown on the page.
Thus, my question is: Is it possible to run all validations on a particular model WITHOUT modifying @errors? Looking at the source, I'm not sure if it is. But there must be some way to do this.
Thanks in advance,