# How remove blank leading spaces before validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247>\
**Category:** Rails\
**Created:** [September 22, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247 "2008-09-22T21:32:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 22, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/1 "2008-09-22T21:32:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 22, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/2 "2008-09-22T21:33:54Z")

</div>

On Sep 23, 3:18 am, “paco.onrails” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 22, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/3 "2008-09-22T21:43:18Z")

</div>

before\_validation :strip\_white\_space

def strip\_white\_space  
self…

---

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 23, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/4 "2008-09-23T15:06:52Z")

</div>

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 F. wrote:

> On Sep 23, 3:18ï¿½am, “paco.onrails” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 23, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/5 "2008-09-23T17:33:23Z")

</div>

> 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 T. wrote:

> On Tue, Sep 23, 2008 at 6:06 AM, Robert W. \<  
> removed\_email\_address@domain.invalid\> 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

---

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 23, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/6 "2008-09-23T16:09:30Z")

</div>

On Tue, Sep 23, 2008 at 6:06 AM, Robert W. \<  
removed\_email\_address@domain.invalid\> 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

---

<div class="post-metadata">

**Author:** ![paco.onrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@paco.onrails](https://www.ruby-forum.com/u/paco.onrails)\
**Post date:** [September 23, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/how-remove-blank-leading-spaces-before-validates-uniqueness-of/149247/7 "2008-09-23T18:52:49Z")

</div>

On Sep 23, 11:33 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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