Created
September 25, 2012 12:31
-
-
Save dpsk/3781497 to your computer and use it in GitHub Desktop.
Partially validate model attributes. Good for multi-step wizards form without saving record on each step.
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
module PartiallyValidatable | |
extend ActiveSupport::Concern | |
included do | |
def self.partially_valid?(params, valid=true) | |
mock = self.new(params) | |
params.each{|attr, _| valid = false if mock.errors.messages[attr.to_sym].present?} unless mock.valid? | |
valid | |
end | |
def self.generate_errors(params, errors=[]) | |
mock = self.new(params) | |
params.each{|attr, _| errors << mock.errors.messages[attr.to_sym] if mock.errors.messages[attr.to_sym].present?} unless mock.valid? | |
errors.flatten | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment