# Using Value Length in Validation Message

**URL:** <https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163>\
**Category:** Rails\
**Created:** [May 17, 2009, 11:14am UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163 "2009-05-17T11:14:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil7085](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@phil7085](https://www.ruby-forum.com/u/phil7085)\
**Post date:** [May 17, 2009, 11:14am UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163/1 "2009-05-17T11:14:06Z")

</div>

I am learning to use Validations and am using validates\_length\_of to  
check a field value is no longer than 240 characters.

I want to be able to produce a message back to the user telling them  
that the value they entered was x characters too long.

Does anyone know how I can work with the value and its length?

validates\_length\_of :name, :maximum =\> 240, :message =\>

---

<div class="post-metadata">

**Author:** ![phil7085](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@phil7085](https://www.ruby-forum.com/u/phil7085)\
**Post date:** [May 17, 2009, 11:52am UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163/2 "2009-05-17T11:52:44Z")

</div>

:message =\> “Length is #{name.length}, max allowed is 240”

2009/5/17 phil7085 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![phil7085](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@phil7085](https://www.ruby-forum.com/u/phil7085)\
**Post date:** [May 17, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163/3 "2009-05-17T14:17:55Z")

</div>

Thanks for the reply, That doesn’t give me the value for name it just  
gives back the name of the model. i.e Task so the length given is 4.

Any ideas?

---

<div class="post-metadata">

**Author:** ![phil7085](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@phil7085](https://www.ruby-forum.com/u/phil7085)\
**Post date:** [May 17, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163/4 "2009-05-17T15:07:10Z")

</div>

2009/5/17 phil7085 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Thanks for the reply, That doesn’t give me the value for name it just  
> gives back the name of the model. i.e Task so the length given is 4.

That’s true, I would go with Frederick’s solution.  
Colin

---

<div class="post-metadata">

**Author:** ![phil7085](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@phil7085](https://www.ruby-forum.com/u/phil7085)\
**Post date:** [May 17, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163/5 "2009-05-17T14:49:00Z")

</div>

On May 17, 1:17 pm, phil7085 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the reply, That doesn’t give me the value for name it just  
> gives back the name of the model. i.e Task so the length given is 4.
> 
> Any ideas?

you could do this with a custom validation, ie

validates do |record|  
if …  
record.errors.add :name, “Name is #{record.name.length} characters  
long”  
end  
end

---

<div class="post-metadata">

**Author:** ![phil7085](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@phil7085](https://www.ruby-forum.com/u/phil7085)\
**Post date:** [May 17, 2009, 3:10pm UTC](https://www.ruby-forum.com/t/using-value-length-in-validation-message/168163/6 "2009-05-17T15:10:06Z")

</div>

Yep that works, not quite sure why i couldn’t access the length before  
though, still abit over my head but i’m learning.

Thanks for your help guys.

P
