Created
March 5, 2012 15:54
-
-
Save ramhoj/1978950 to your computer and use it in GitHub Desktop.
Custom validation objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ParentRegistartionValidator < ActiveModel::Validator | |
| def validate(record) | |
| record.errors.add(:weight, :blank) unless record.weight.present? | |
| ... | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Registration | |
| validates_with ParentRegistartionValidator, :if => proc { |r| r.validator == "parent_registration" } | |
| attr_accessor :validator | |
| ... | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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