Created
July 11, 2011 13:02
-
-
Save gicappa/1075785 to your computer and use it in GitHub Desktop.
Custom validator
This file contains 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
# encoding: UTF-8 | |
module ActiveModel | |
# == Active Model Exclusion Validator | |
module Validations | |
class VersionValidator < EachValidator | |
def validate_each(record, attribute, value) | |
record.errors.add(attribute, "Non può essere vuoto") if value.nil? or value.strip.empty? | |
record.errors.add(attribute, "La version non è espressa nel formato 2.3.4") unless value =~ /\b\d+\.\d+\.\d+\b/ | |
end | |
end | |
module HelperMethods | |
def validates_version_of(*attr_names) | |
validates_with VersionValidator, _merge_attributes(attr_names) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment