user290870
user290870

Reputation: 1601

rails validate_format_of non-negative integers

I am trying to validate the format of non-negative integers with the following

validates_format_of :fundays, :with => /\A[\d]+\Z/, :message => "invalid fundays"

And here is the form field used in the view

<%= f.text_field :fundays, :maxlength => 3, :style => 'width:50px;' %>

However, when I input a non-digit into this field and submit the form, it does not fail the validation. Instead it saves a value of 0 in the database. How do I make it write to the list of error messages.

thanks

Upvotes: 2

Views: 3342

Answers (1)

Salil
Salil

Reputation: 47472

validates_numericality_of :fundays, :only_integer =>true, 
                          :greater_than_or_equal_to =>0, 
                          :message => "invalid fundays"

Upvotes: 8

Related Questions