I wanted to have custom error messages for my field names. I stumbled upon another SO question
So I added something like this:
class Product < ActiveRecord::Base
validate do |prod|
prod.errors.add_to_base("Product price can't be blank") if prod.prod_price.blank?
end
end
But I also want to check the numericality of prod_price. If I just add
validate_numericality_of :prod_price
and product price is empty then both the error messages show up (empty and is not a number).
How can I just have 'is not a number' error message show up only when product price is NOT empty?
I tried doing
class Product < ActiveRecord::Base
validate do |prod|
prod.errors.add_to_base("Product price can't be blank") if prod.prod_price.blank?
if !prod.prod_price.blank?
prod.errors.add_to_base("Product price must be a number") if prod.prod_price.<whatdo i put here>
end
end
end
Also, How can I have a custom message for 'is not a number'. I want to hide showing my column name to the user.