# Form validation

**URL:** <https://www.ruby-forum.com/t/form-validation/140996>\
**Category:** Rails\
**Created:** [June 20, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/form-validation/140996 "2008-06-20T16:51:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajitonrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ajitonrails](https://www.ruby-forum.com/u/ajitonrails)\
**Post date:** [June 20, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/form-validation/140996/1 "2008-06-20T16:51:04Z")

</div>

Hello all

i have a registration form. i am validating say, password field using

validates\_presence\_of :password, :if =\> :password\_required?  
validates\_presence\_of :password\_confirmation, :if  
=\> :password\_required?  
validates\_length\_of :password, :within =\> 4…40, :if  
=\> :password\_required?  
validates\_confirmation\_of :password, :if =\> :password\_required?

now in my form while submitting, if i wont put password i got 4 error  
messages:

password is required  
password length should be min 4 chars. and so on

my question is, Is there a way to customize these error messages? all  
i need is just one error message per field.

any suggestion?

thanks in advance.

Ajit

---

<div class="post-metadata">

**Author:** ![ajitonrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ajitonrails](https://www.ruby-forum.com/u/ajitonrails)\
**Post date:** [June 21, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/form-validation/140996/2 "2008-06-21T15:16:04Z")

</div>

Ajit Singh wrote:

> Hello all
> 
> i have a registration form. i am validating say, password field using
> 
> validates\_presence\_of :password, :if =\> :password\_required?  
> validates\_presence\_of :password\_confirmation, :if  
> =\> :password\_required?  
> validates\_length\_of :password, :within =\> 4…40, :if  
> =\> :password\_required?  
> validates\_confirmation\_of :password, :if =\> :password\_required?
> 
> now in my form while submitting, if i wont put password i got 4 error  
> messages:
> 
> password is required  
> password length should be min 4 chars. and so on
> 
> my question is, Is there a way to customize these error messages? all  
> i need is just one error message per field.
> 
> any suggestion?
> 
> thanks in advance.
> 
> Ajit

You should use the :unless option of the validates method in  
“validates\_length\_of :password”. This option specifies a method to call  
to determine if the validation should not occur.

validates\_length\_of :password, :unless =\> Proc.new{|o|  
o.password.blank?}
