# Usage: # # class MyClass < ActiveRecord::Base # ... # validate :postal_code, spanish_postal_code: true # default message # validate :postal_code, spanish_postal_code: { message: '' } # custom message # ... # end # class SpanishPostalCodeValidator < ActiveModel::EachValidator def validate_each(object, attribute, value) unless value =~ /\A(0[1-9]|[1-4][0-9]|5[0-2])\d{3}\z/i object.errors[attribute] << options.fetch(:message, I18n.t('errors.messages.invalid')) end end end