Hi,
I''ve got a Contact model containing phone numbers (land,mobile,fax) and
I''d like to be able to non-digit characters from the form data before
the in-model validation is applied. This is easy to do in the controller
before I construct the Contact object with something like:
params[:contact][''mobile''].tr!(''^0-9'','''')
However, I''d really like the Contact model to manage its own
validation.
Is there a way to do this simply by overriding the mobile= method
itself, or do I need to write my own method to build the whole model
from parameters, eg: self.from_params(params) ?
Doing
def mobile=(mob_str)
self.mobile = mob_str.tr!(''^0-9'','''')
end
just endlessly recurses with repeated assignments...
thanks,
donnchaaa
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
On 7 Sep 2006, at 11:53, donncha wrote:> However, I''d really like the Contact model to manage its own > validation. > Is there a way to do this simply by overriding the mobile= method > itself, or do I need to write my own method to build the whole model > from parameters, eg: self.from_params(params) ?Look into some of Rails models'' built-in hooks -- in your case, I think before_validate would be a good choice. before_validate :cleanup_mobile_number def cleanup_mobile_number mobile = .... end Scott -- http://www.matthewman.net/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Scott Matthewman wrote:> before_validate :cleanup_mobile_numberThanks Scott. Worked perfectly ;-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---