Class: ActiveModel::Validations::OnlyNumbersValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/moca_rlibs/active_model_validators/only_numbers.rb

Overview

文字列が数字のみであることをチェックする

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/moca_rlibs/active_model_validators/only_numbers.rb', line 9

def validate_each(record, attribute, value)
  return if value.blank?

  value = value.presence.to_s
  return if value.match?(/\A[0-9]*\z/)

  record.errors.add('contain invalid characters (only numbers)')
end