Forked from backpackerhh/spanish_postal_code_validator.rb
Created
September 16, 2019 15:53
-
-
Save Altons/d5ee1120cc0adb59ddac8bfd2a254d8b to your computer and use it in GitHub Desktop.
Revisions
-
backpackerhh revised this gist
May 19, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,8 @@ # # class MyClass < ActiveRecord::Base # ... # validate :postal_code, spanish_postal_code: true # default message # validate :postal_code, spanish_postal_code: { message: '<Your message>' } # custom message # ... # end # -
backpackerhh created this gist
May 17, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ # Usage: # # class MyClass < ActiveRecord::Base # ... # validate :postal_code, spanish_postal_code: true # ... # 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