I want to sanitise a phone number input. In my UsersController.rb file I have written a method that will take a string and reduce it to numbers only. This works find if I call it manually, but in the interests of keeping DRY I would like to ensure that the code sanitisation method is called whenever the data is edited (I''m using standard scaffolidng for this test). Is there anyway I can do this and keep the code overhead low? My current sanitisation method looks like this: private def numericalise!(*number) for n in number.phone do n.gsub!(/[^0-9]/, '''') end end Thanks in advance for your time and help. -- Posted via http://www.ruby-forum.com/.
Why not use validates_format_of http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html On Monday, April 24, 2006, at 9:52 PM, Michael Ward wrote:>I want to sanitise a phone number input. > >In my UsersController.rb file I have written a method that will take a >string and reduce it to numbers only. > >This works find if I call it manually, but in the interests of keeping >DRY I would like to ensure that the code sanitisation method is called >whenever the data is edited (I''m using standard scaffolidng for this >test). > >Is there anyway I can do this and keep the code overhead low? > >My current sanitisation method looks like this: > > private > def numericalise!(*number) > for n in number.phone do > n.gsub!(/[^0-9]/, '''') > end > end > > >Thanks in advance for your time and help. > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
I want to remove thr formatting, not check for it. It''s to be expected that user may put spaces, commas, periods and parenthisis in a phone number and I don''t want to force them in to a particular format. If I sanitise the code first (then validate it for completeness) then I''m happy with the data and the user finds the input easy. Kevin Olbrich wrote:> Why not use > > validates_format_of > > http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html > > On Monday, April 24, 2006, at 9:52 PM, Michael Ward wrote: >>Is there anyway I can do this and keep the code overhead low? >> >>Thanks in advance for your time and help. >> >>-- >>Posted via http://www.ruby-forum.com/. >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > _Kevin-- Posted via http://www.ruby-forum.com/.
Then just call your sanitizer on the attribute in question in the update or create methods of your controller. On Monday, April 24, 2006, at 10:22 PM, Michael Ward wrote:>I want to remove thr formatting, not check for it. It''s to be expected >that user may put spaces, commas, periods and parenthisis in a phone >number and I don''t want to force them in to a particular format. > >If I sanitise the code first (then validate it for completeness) then >I''m happy with the data and the user finds the input easy. > >Kevin Olbrich wrote: >> Why not use >> >> validates_format_of >> >> http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html >> >> On Monday, April 24, 2006, at 9:52 PM, Michael Ward wrote: >>>Is there anyway I can do this and keep the code overhead low? >>> >>>Thanks in advance for your time and help. >>> >>>-- >>>Posted via http://www.ruby-forum.com/. >>>_______________________________________________ >>>Rails mailing list >>>Rails@lists.rubyonrails.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> _Kevin > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Michael Ward wrote:> I want to remove thr formatting, not check for it. It''s to be expected > that user may put spaces, commas, periods and parenthisis in a phone > number and I don''t want to force them in to a particular format. > > If I sanitise the code first (then validate it for completeness) then > I''m happy with the data and the user finds the input easy.You could use the before_validation callback to clean the data before it gets validated. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Thankyou, this was what I needed :) Josh Susser wrote:> Michael Ward wrote: >> I want to remove thr formatting, not check for it. It''s to be expected >> that user may put spaces, commas, periods and parenthisis in a phone >> number and I don''t want to force them in to a particular format. >> >> If I sanitise the code first (then validate it for completeness) then >> I''m happy with the data and the user finds the input easy. > > You could use the before_validation callback to clean the data before it > gets validated. > > -- > Josh Susser > http://blog.hasmanythrough.com-- Posted via http://www.ruby-forum.com/.