Skip to content

Instantly share code, notes, and snippets.

@ramhoj
Created March 5, 2012 15:54
Show Gist options
  • Select an option

  • Save ramhoj/1978950 to your computer and use it in GitHub Desktop.

Select an option

Save ramhoj/1978950 to your computer and use it in GitHub Desktop.
Custom validation objects
class ParentRegistartionValidator < ActiveModel::Validator
def validate(record)
record.errors.add(:weight, :blank) unless record.weight.present?
...
end
end
class Registration
validates_with ParentRegistartionValidator, :if => proc { |r| r.validator == "parent_registration" }
attr_accessor :validator
...
end
class RegistrationWizardController
def update
@registration = Registration.find(params[:id])
@registration.validator = "parent_registration"
if @registration.update_attribute(params[:registration])
redirect_to(url)
else
render(:edit)
end
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment