paco.onrails
2008-Sep-22 19:18 UTC
how remove blank leading spaces before validates_uniqueness_of
Hi again, I have a problem when I try to validate uniqueness of data entered through text fields in this particular circumstance. Let say that I have a text field which has a validates_uniqueness_of on its respective model. And let say that the user enter for that field the value "Stanley Kubrick" and it is saved without problems. On the listing I will see the entered value "Stanley Kubrick", as it was supposed to be. Then I use the same form to enter the value "Stanley Kubrick " (with a blank space at the end) and, obviously, validation will let it pass. The problem begins when the user will see on the listing two "apparently" duplicated records like this: - Stanley Kubrick - Stanley Kubrick What I need is a short way to remove those leading blank spaces before the validation that doesn''t push me to change a hundred of models in my applications. Peace!! --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Luther
2008-Sep-22 19:30 UTC
Re: how remove blank leading spaces before validates_uniqueness_of
before_validation :strip_white_space def strip_white_space self........ On Sep 22, 12:18 pm, "paco.onrails" <pacorey...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi again, I have a problem when I try to validate uniqueness of data > entered through text fields in this particular circumstance. > Let say that I have a text field which has a validates_uniqueness_of > on its respective model. > And let say that the user enter for that field the value "Stanley > Kubrick" and it is saved without problems. > > On the listing I will see the entered value "Stanley Kubrick", as it > was supposed to be. > > Then I use the same form to enter the value "Stanley Kubrick " (with a > blank space at the end) and, obviously, validation will let it pass. > The problem begins when the user will see on the listing two > "apparently" duplicated records like this: > > - Stanley Kubrick > - Stanley Kubrick > > What I need is a short way to remove those leading blank spaces before > the validation that doesn''t push me to change a hundred of models in > my applications. > > Peace!!--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Erol Fornoles
2008-Sep-22 19:32 UTC
Re: how remove blank leading spaces before validates_uniqueness_of
On Sep 23, 3:18 am, "paco.onrails" <pacorey...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi again, I have a problem when I try to validate uniqueness of data > entered through text fields in this particular circumstance. > Let say that I have a text field which has a validates_uniqueness_of > on its respective model. > And let say that the user enter for that field the value "Stanley > Kubrick" and it is saved without problems. > > On the listing I will see the entered value "Stanley Kubrick", as it > was supposed to be. > > Then I use the same form to enter the value "Stanley Kubrick " (with a > blank space at the end) and, obviously, validation will let it pass. > The problem begins when the user will see on the listing two > "apparently" duplicated records like this: > > - Stanley Kubrick > - Stanley Kubrick > > What I need is a short way to remove those leading blank spaces before > the validation that doesn''t push me to change a hundred of models in > my applications. > > Peace!!Try this: class Person before_validation :strip_blanks protected def strip_blanks self.name = self.name.strip end end --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2008-Sep-23 13:06 UTC
Re: how remove blank leading spaces before validates_uniquen
Although this is a reasonably clean and straight forward solution, is there a compelling reason that Rails itself doesn''t trim leading and trailing whitespace from text fields? It seems to me that would be a reasonable "intelligent" default. How often would you want to preserve the leading and trailing whitespace entered by a user? Wouldn''t it make sense instead to have a way of overriding the trimming only when necessary? Anyway, just some thoughts. This is one of two things that I personally wish was done differently in Rails form processing. The second is that I would prefer that fields containing nothing (or whitespace only) were interpreted as nil rather than an empty string. I don''t like having empty strings in my database where I would expect the value to be NULL. However, I realize that others may not agree with me on this one, so maybe have an application wide configuration setting to tell Rails how to interpret empty text fields. I suppose there might also be a way to implement this type of behavior in a before_filter of the application controller. I''ll have to do some experimenting with that... Erol Fornoles wrote:> On Sep 23, 3:18�am, "paco.onrails" <pacorey...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Then I use the same form to enter the value "Stanley Kubrick " (with a >> >> Peace!! > > Try this: > > class Person > before_validation :strip_blanks > > protected > > def strip_blanks > self.name = self.name.strip > end > end-- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2008-Sep-23 13:54 UTC
Re: how remove blank leading spaces before validates_uniquen
On Tue, Sep 23, 2008 at 6:06 AM, Robert Walker < rails-mailing-list@andreas-s.net> wrote:> > Although this is a reasonably clean and straight forward solution, is > there a compelling reason that Rails itself doesn't trim leading and > trailing whitespace from text fields? It seems to me that would be a > reasonable "intelligent" default. How often would you want to preserve > the leading and trailing whitespace entered by a user? > > Wouldn't it make sense instead to have a way of overriding the trimming > only when necessary? Anyway, just some thoughts. > > This is one of two things that I personally wish was done differently in > Rails form processing. The second is that I would prefer that fields > containing nothing (or whitespace only) were interpreted as nil rather > than an empty string. I don't like having empty strings in my database > where I would expect the value to be NULL. However, I realize that > others may not agree with me on this one, so maybe have an application > wide configuration setting to tell Rails how to interpret empty text > fields. > > I suppose there might also be a way to implement this type of behavior > in a before_filter of the application controller. I'll have to do some > experimenting with that... >You have a good idea but I don't think that you want to start modifying the standard functionality of Rails helpers that are to mirror the standard HTML tags. Also, it seems that you have written more in your argument to add trimming to the text field tag than it took to actually write the ruby code to do it. :-) In short, I would leave the post processing of input to the methods that are tasked to do it. -Conrad> > Erol Fornoles wrote: > > On Sep 23, 3:18�am, "paco.onrails" <pacorey...@gmail.com> wrote: > >> Then I use the same form to enter the value "Stanley Kubrick " (with a > >> > >> Peace!! > > > > Try this: > > > > class Person > > before_validation :strip_blanks > > > > protected > > > > def strip_blanks > > self.name = self.name.strip > > end > > end > > -- > 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2008-Sep-23 15:33 UTC
Re: how remove blank leading spaces before validates_uniquen
> In short, I would > leave the post processing of input to the methods that are tasked to do > it.I''m sure you''re right. I was just sorting of thinking out loud, fishing for other people''s opinions. It just seemed to me that some basic pre-processing of user input by the Rails subsystem could alleviate having to handle these issues on a nearly-every-case basis. I can''t think of too many cases where I would want to preserve the leading and trailing whitespace from a text field. So I''d rather have to add some extra code in order to preserve whitespace, rather than adding code to practically every model to ensure I''m getting them trimmed. I think it would be actually be nice if accessing something like params[:some_value] for :some_value to be trimmed of leading and trailing whitespace unless told otherwise. Conrad Taylor wrote:> On Tue, Sep 23, 2008 at 6:06 AM, Robert Walker < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> This is one of two things that I personally wish was done differently in >> experimenting with that... >> > > You have a good idea but I don''t think that you want to start modifying > the > standard functionality of Rails helpers that are to mirror the standard > HTML > tags. > Also, it seems that you have written more in your argument to add > trimming > to the > text field tag than it took to actually write the ruby code to do it. > :-) > In short, I would > leave the post processing of input to the methods that are tasked to do > it. > > -Conrad-- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Erol Fornoles
2008-Sep-23 16:52 UTC
Re: how remove blank leading spaces before validates_uniquen
On Sep 23, 11:33 pm, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m sure you''re right. I was just sorting of thinking out loud, fishing > for other people''s opinions. > > It just seemed to me that some basic pre-processing of user input by the > Rails subsystem could alleviate having to handle these issues on a > nearly-every-case basis. > > I can''t think of too many cases where I would want to preserve the > leading and trailing whitespace from a text field. So I''d rather have to > add some extra code in order to preserve whitespace, rather than adding > code to practically every model to ensure I''m getting them trimmed. > > I think it would be actually be nice if accessing something like > params[:some_value] for :some_value to be trimmed of leading and > trailing whitespace unless told otherwise.Rails is probably just following the principle of least astonishment. Most devs, or atleast those I know of, still expects user input to be saved as is. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---