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!!

before_validation :strip_white_space

def strip_white_space self........

Try this:

class Person   before_validation :strip_blanks

  protected

  def strip_blanks     self.name = self.name.strip   end end

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:

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. :slight_smile: In short, I would

leave the post processing of input to the methods that are tasked to do it.

-Conrad

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:

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.