ExiRe
ExiRe

Reputation: 4767

Rails 3 - own validation message isntead of "is not a number"

I want to check in my app, that input value should be integer and couldn't be empty or nil.

class Event < ActiveRecord::Base
  validates :event_cost, :presence => { :message => "can't be empty..." }

  validates :event_cost, 
              :numericality => { :only_integer => { :message => "should be only integer" } } 
end

So, if user prints nothing in the field i get message "Event cost is not a number". How can i set my own message instead of that?

Upvotes: 2

Views: 883

Answers (3)

Kashiftufail
Kashiftufail

Reputation: 10885

Try out this

validates :event_cost, :numericality => { :only_integer => true,{ :message => "should be only integer" } }

Upvotes: 1

Luca
Luca

Reputation: 4273

Try

class Event < ActiveRecord::Base
  validates :event_cost, :presence => { :message => "can't be empty..." }

  validates :event_cost, 
              :numericality => { :message => "Your own message", :only_integer => true } 
end

Upvotes: 5

abhas
abhas

Reputation: 5213

Try this one it will work fine as you didn't set only_integer to true it was not working properly

:event_cost, 
          :numericality => { :only_integer => true, :message => "should be only integer"}

Upvotes: 1

Related Questions