0

I am trying to validate the entry a user makes in an amount field.

The field is amount_money

This field is a string which is validated on form submission

 monetize :amount, :as => :amount_money
 validates :amount, numericality: {only_integer: true}
 validates :amount_money, numericality: {greater_than_or_equal_to: 0}
 validate :amount_money_within_limit
 validate :is_a_valid_number

I want to ensure there are no letters or symbols and that the amount is in an acceptable range.

the code to do this is

def amount_money_within_limit
    if amount_money && amount_money.cents > 10_000_00 
        errors.add(:amount_money, 'cannot exceed $10,000.')
    end
    if amount_money && amount_money.cents < 1_00 
      errors.add(:amount_money, 'Problem with Amount')
    end
end

this works great for input numbers, of numbers and letters, of letters, of special characters but

If I try Bob - the validation kicks in but if I try BBob - the validation is bypassed.

If the input contains 2 Capital letters next to each other - it fails. I've tried a downcase - but that doesn't suit as the field is monetized (money gem) - and the downcase screws up if there is valid input.

If the input to the field contains two uppercase letters - all the validations are bypassed So something like AA is not caught by any on the above validations

techguy
  • 47
  • 2
  • 9
  • look [here](http://guides.rubyonrails.org/active_record_validations.html#numericality) – Sam Sep 12 '15 at 16:55
  • What is `amount_money`? How is it defined? How is it assigned? You talk about trying `"Bob"`, but you presumably mean a string, and string's don't have `cents` methods. Please add more of the relevant code. – eirikir Sep 12 '15 at 16:55
  • note that 1_00 is different from 1.00. 1_00 = 100 – WeezHard Sep 12 '15 at 17:15
  • the validation methods pointed to by Sam D do not explain why the validations I am using work for a mix of letters and numbers, and for characters - but do not work if there are two capital letters. Regarding 1_00 being 100 - the point would be that DD (or any other non numeric combination would be less than "a number" – techguy Sep 12 '15 at 17:29
  • Do you use this gem https://github.com/RubyMoney/money-rails? – Alexey Shein Sep 12 '15 at 19:08

2 Answers2

1

Why don't you use regular expressions? Something like this:

def is_a_valid_number? amount_money
  amount_money =~ /\d+/
end
Nikola Todorovic
  • 300
  • 3
  • 12
  • Ruby is not my native language - so I am having a problem with implementing this. I also need it to be in a validation format so if {validation check fails} present graceful error end and then this would be called when the form is submitted – techguy Sep 12 '15 at 17:11
  • You can use regular expressions in any language. If you are new to Ruby than you first need to read about validations in Rails, if that doesn't help you then use regexp. – Nikola Todorovic Sep 12 '15 at 17:20
1

It seems you have put 1 validation on the wrong field, you should've put validations only on amount field (your real db field), and not on the amount_money which is automagical field from rails-money gem. I'll apply their documentation on numerical validations to your case:

monetize :amount,
  :numericality => {
    :only_integer => true,
    :greater_than_or_equal_to => 1_00,
    :less_than_or_equal_to => 10_000_00
  }

You won't need any other custom validations with this setup.

Alexey Shein
  • 7,342
  • 1
  • 25
  • 38