# Validation order when mixing callbacks and validates\_format

**URL:** <https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670>\
**Category:** Rails\
**Created:** [December 31, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670 "2007-12-31T22:51:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [December 31, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670/1 "2007-12-31T22:51:55Z")

</div>

I posted this question the other day, but it was pretty late so I think  
I wrote a bunch of gibberish. So let me try it now that I’ve been  
properly doused with caffeine. 🙂

The Problem: I’m using the “before\_validation” callback to modify an  
attribute value on a particular model object before it gets validated.  
I’m also using “validates\_format\_of” to do error checking on that same  
model attribute. If “before\_validation” doesn’t happen before  
“validates\_format\_of”, Rails blows up.

Looking in my AWDWR book, it looks like when you call model.save(),  
there is an order to validation:

before\_validation  
…  
after\_validation  
…  
before\_save  
before\_create / before\_update  
INSERT / UPDATE OPERATION  
…

However, when I try to create a new model object, it looks like  
before\_validation does not get called before “validates\_format\_of”. My  
assumption would be that the “validates\_” helpers should before after  
“before\_validation”, but this doesn’t seem to be correct.

Can anyone help me out with this? When does “validates\_format\_of” get  
called in relation to “before\_validation”?

Thanks,  
kodama

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [December 31, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670/2 "2007-12-31T23:45:39Z")

</div>

Can you post your before\_validation code?

What actually blows up? Can you also post the relevant lines from the  
log?

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [October 28, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670/3 "2008-10-28T20:23:42Z")

</div>

brewpoo wrote:

> Can you post your before\_validation code?
> 
> What actually blows up? Can you also post the relevant lines from the  
> log?

I am having this same problem. It seems the before\_validation isn’t  
working in mine:

```
before_validation :normalize_phone_number

validates_format_of :number, :with => /^[0-9]{10,15}$/

protected

def normalize_phone_number
  self.number.gsub(/-/,"").gsub(/ /,"")
end

```

The object won’t save since it fails the validates\_format\_of since it  
never runs the before\_validation. I’m running Rails 2.1.0.

Any ideas?

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [October 28, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670/4 "2008-10-28T21:47:48Z")

</div>

I just figured out what I was doing wrong.

I was using “gsub” instead of “gsub!”

now it is working fine

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [June 19, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/validation-order-when-mixing-callbacks-and-validates-format/124670/5 "2009-06-19T13:06:08Z")

</div>

I’m having the same problem. I want to allow people to enter a value  
including a $ or Â£. With this method, the value of the attribute in  
before\_validation is zero if I enter a value with text (or a $) at the  
start.

So entering $1000 is turned into 0, before before\_validation.

Any ideas?

Marli Ba wrote:

> brewpoo wrote:
> 
> > Can you post your before\_validation code?
> > 
> > What actually blows up? Can you also post the relevant lines from the  
> > log?
> 
> I am having this same problem. It seems the before\_validation isn’t  
> working in mine:
> 
> ```
> before_validation :normalize_phone_number
> 
> validates_format_of :number, :with => /^[0-9]{10,15}$/
> 
> protected
> 
> def normalize_phone_number
> self.number.gsub(/-/,"").gsub(/ /,"")
> end
> 
> ```
> 
> The object won’t save since it fails the validates\_format\_of since it  
> never runs the before\_validation. I’m running Rails 2.1.0.
> 
> Any ideas?
