Set attribute value from custom validation

Is there any way to set a value from a custom validates?

There are times when a user has submitted the appropriate information,
but just not in the exact form I am looking for and I would like to
override what they entered without generating a warning.

For example, I validating phone numbers and have defined
“validates_us_phone”. As long as the entry has 10 or 11 digits before
an optional ‘x’ (for extension), I’d like to accept the entry, but
modify it before saving it.

This way, people can enter:

1 123 456-7890
123-456-7890 x123

and the validates would change their entries to


The validates_us_phone function is written and works up to the point
where the new value overrides the supplied value.

Given the following code near the top of validates_us_phone:

validates_each attr_names do |m, a, v|

are there any methods available that would let me set the value of the

I am guessing that the alternative would be to break this into two

  • Validate the entry, but don’t change the value
  • Add a function in before_save that would assume the entry is valid
    and would massage it into the correct format


  • Tony

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs