Validate identity code on user

This commit is contained in:
Martin Lensment 2014-10-07 17:21:45 +03:00
parent b403cbebcd
commit 9f878f219c
2 changed files with 11 additions and 0 deletions

View file

@ -16,6 +16,8 @@ class User < ActiveRecord::Base
validates :email, presence: true, if: -> { country.iso != 'EE' }
validates :registrar, presence: true, if: -> { !admin }
validate :validate_identity_code
before_save -> { self.registrar = nil if admin? }
attr_accessor :registrar_typeahead
@ -27,4 +29,12 @@ class User < ActiveRecord::Base
def registrar_typeahead
@registrar_typeahead || registrar || nil
end
private
def validate_identity_code
return unless identity_code.present?
code = Isikukood.new(identity_code)
errors.add(:identity_code, :invalid) unless code.valid?
end
end

View file

@ -170,6 +170,7 @@ en:
password:
blank: 'Password is missing'
identity_code:
invalid: 'Identity code is invalid'
blank: 'Identity code is missing'
taken: 'Identity code already exists'
email: