Skip to content

Instantly share code, notes, and snippets.

@gicappa
Created July 11, 2011 13:02
Show Gist options
  • Save gicappa/1075785 to your computer and use it in GitHub Desktop.
Save gicappa/1075785 to your computer and use it in GitHub Desktop.
Custom validator
# 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