# Validating an individual field (Rails 3)

**URL:** <https://www.ruby-forum.com/t/validating-an-individual-field-rails-3/186896>\
**Category:** Rails\
**Created:** [April 13, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/validating-an-individual-field-rails-3/186896 "2010-04-13T22:45:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cult\_hero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@cult\_hero](https://www.ruby-forum.com/u/cult_hero)\
**Post date:** [April 13, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/validating-an-individual-field-rails-3/186896/1 "2010-04-13T22:45:17Z")

</div>

I’m working on a form using a model that isn’t tied to a database or  
ORM at all and using ActiveModel::Validations works like a charm. I’m  
very pleased with it thus far.

However, I can’t find an obvious way to validate a single field rather  
than the whole model. Is there a simple way to do this?

---

<div class="post-metadata">

**Author:** ![cult\_hero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@cult\_hero](https://www.ruby-forum.com/u/cult_hero)\
**Post date:** [April 14, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/validating-an-individual-field-rails-3/186896/2 "2010-04-14T15:54:59Z")

</div>

I am not aware of such a feature but you could write your own  
validation method in the model and invoke it like any other method  
after the field is populated.

---

<div class="post-metadata">

**Author:** ![cult\_hero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@cult\_hero](https://www.ruby-forum.com/u/cult_hero)\
**Post date:** [April 14, 2010, 6:17pm UTC](https://www.ruby-forum.com/t/validating-an-individual-field-rails-3/186896/3 "2010-04-14T18:17:52Z")

</div>

I tried to figure out how .valid? did it by tracing back through the  
code and ultimate found a method being called:

\_run\_validate\_callbacks

Unfortunately, I couldn’t find the actual def anywhere because I’m  
guessing something in ActiveSupport::Callbacks creates the method. I  
didn’t have a desire to go through all the metaprogramming, but after  
some digging I found enough to write this method:

```
def validate_field(field)
  field = field.to_sym
  if self._validators[field]
    self.errors.clear
    self._validators[field].each { |v| v.validate self }
    self.errors[field]
  else
    nil
  end
end

```

The reason for this method is that I have a form that validates as you  
fill it out via AJAX. One of the validation calls is pretty expensive  
so I don’t want it called every time I do something like check to see  
if the passwords match.
