# Field validation

**URL:** <https://www.ruby-forum.com/t/field-validation/190125>\
**Category:** Rails\
**Created:** [June 16, 2010, 12:34pm UTC](https://www.ruby-forum.com/t/field-validation/190125 "2010-06-16T12:34:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 16, 2010, 12:34pm UTC](https://www.ruby-forum.com/t/field-validation/190125/1 "2010-06-16T12:34:10Z")

</div>

Hi  
I have a field called “cost”:

\<%= f.text\_field :cost, :size=\>2, :maxlength=\>4 %\>

I want to check that user enter numeral value and this value is integer

In post.rb (model):

```
def cost_validate_int
  errors.add_to_base("Error message") unless cost.is_a?(Fixnum)
  puts "### #{cost}: "+cost.class.to_s
end

```

When I enter in field 100 I get:

### 100: Fixnum

when I enter 10.1:

### 10: Fixnum

when I enter ‘asdf’:

### 0: Fixnum

I completely can’t understand what happen. In migration:

t.integer :cost

I think ActiveRectord converts value to Fixnum because in migration this  
field signed as integer

Can I in some way check cost value in model (value should be numeral and  
integer)?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 16, 2010, 12:43pm UTC](https://www.ruby-forum.com/t/field-validation/190125/2 "2010-06-16T12:43:21Z")

</div>

Stanislav O. wrote:

> Can I in some way check cost value in model (value should be numeral and  
> integer)?
> 
> Thanks in advance!

validates\_numericality\_of?

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 16, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/field-validation/190125/3 "2010-06-16T13:19:07Z")

</div>

Pale H. wrote:

> Stanislav O. wrote:
> 
> > Can I in some way check cost value in model (value should be numeral and  
> > integer)?
> > 
> > Thanks in advance!
> 
> validates\_numericality\_of?

I use errors.add\_to\_base because I don’t want to show field’s name in  
the error message(i. e. Cost bla bla bla. web site is localized).

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/field-validation/190125/4 "2010-06-17T16:22:42Z")

</div>

On 16 June 2010 11:34, Stanislav O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> errors.add_to_base("Error message") unless cost.is_a?(Fixnum)
> 
> ```
> 
> when I enter ‘asdf’:
> 
> ### 0: Fixnum
> 
> I completely can’t understand what happen. In migration:
> 
> t.integer :cost
> 
> I think ActiveRectord converts value to Fixnum because in migration this  
> field signed as integer

How is the string you enter getting into the cost attribute?

Colin

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 17, 2010, 5:15pm UTC](https://www.ruby-forum.com/t/field-validation/190125/5 "2010-06-17T17:15:49Z")

</div>

well… in fact… you did defined it as an “integer” so, dont expect to  
get  
any other class thats not Fixnum or Bignum…

but, you can also use some model validations to be sure that the user  
entered an integer…

try validates\_numericality\_of :cost  
it will stop your user from entering strings…

i`m not sure how to stop it from entering floats like "10.6", but i`m  
sure  
there’s a simple way of doing it…

you know, you’re programming in ruby on rails, it IS simple.  
Sempre Alerta Para Servir,

Lucas Franceschi  
Equipe de ProgramaÃ§Ã£o (AutomaÃ§Ã£o)  
SGI Sistemas  
(049) 9922-3360

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 17, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/field-validation/190125/6 "2010-06-17T16:35:09Z")

</div>

Colin L. wrote:

> On 16 June 2010 11:34, Stanislav O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ ï¿½errors.add\_to\_base(“Error message”) unless cost.is\_a?(Fixnum)
> > 
> > when I enter ‘asdf’:
> > 
> > ### 0: Fixnum
> > 
> > I completely can’t understand what happen. In migration:
> > 
> > t.integer :cost
> > 
> > I think ActiveRectord converts value to Fixnum because in migration this  
> > field signed as integer
> 
> How is the string you enter getting into the cost attribute?
> 
> Colin

I can’t understand why cost field value (10, 10.1 or ‘asdf’) all time is  
Fixnum in model. In this model I try to validate cost, and can’t because  
all time I get only one type - Fixnum!

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 17, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/field-validation/190125/7 "2010-06-17T17:17:00Z")

</div>

try looking  
Here[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001401](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001401)  
.  
Sempre Alerta Para Servir,

Lucas Franceschi  
Equipe de ProgramaÃ§Ã£o (AutomaÃ§Ã£o)  
SGI Sistemas  
(049) 9922-3360
