Forum: Ruby on Rails Set attribute value from custom validation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
75ae2e039c201d882f0f8c7d31758ad8?d=identicon&s=25 Tony (Guest)
on 2007-04-25 15:26
(Received via mailing list)
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 topic is locked and can not be replied to.