# Validation problems

**URL:** <https://www.ruby-forum.com/t/validation-problems/183213>\
**Category:** Rails\
**Created:** [February 10, 2010, 1:03am UTC](https://www.ruby-forum.com/t/validation-problems/183213 "2010-02-10T01:03:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 10, 2010, 1:03am UTC](https://www.ruby-forum.com/t/validation-problems/183213/1 "2010-02-10T01:03:24Z")

</div>

Hi, there.  
I have two validations in the model:

validates\_numericality\_of :value, :only\_integer=\>true,  
:allow\_blank=\>true  
validates\_size\_of :value, :is=\>9, :message=\>“must be 5-digit number”,  
:if=\>Proc.new{|u| u.value.is\_a?(Numeric)}

They work as expected except when the :value is character/string like  
“a” “abc”, the second validation will also output error message “must be  
5-digit number”. What I want is the second validation displays error  
message only when the :value is a number. I think :if=\>Proc.new{|u|  
u.value.is\_a?(Numeric)} would take care of it, but it didn’t.

The second problem is about the custom validation. I got a very simple  
custom validation. Whenever there is an error, it catches it and shows  
the error message. But the field with error is not highlighted. I  
checked the source of the page, the “fieldwitherror” was not generated  
on the field checked by the custom validation. I tried the  
validation\_reflection plugin. It catched the error but there’s no error  
message under the field. Normally that’s where the message should be.  
I guess they are related but I am not sure what causes it.

Any help will be appreciated.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 10, 2010, 2:03am UTC](https://www.ruby-forum.com/t/validation-problems/183213/2 "2010-02-10T02:03:07Z")

</div>

> validates\_numericality\_of :value, :only\_integer=\>true,  
> :allow\_blank=\>true  
> validates\_size\_of :value, :is=\>9, :message=\>“must be 5-digit number”,  
> :if=\>Proc.new{|u| u.value.is\_a?(Numeric)}

Try it as u.is\_a?(Numeric)

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 10, 2010, 2:16am UTC](https://www.ruby-forum.com/t/validation-problems/183213/3 "2010-02-10T02:16:30Z")

</div>

E. Litwin wrote:

> > validates\_numericality\_of :value, :only\_integer=\>true,  
> > :allow\_blank=\>true  
> > validates\_size\_of :value, :is=\>9, :message=\>“must be 5-digit number”,  
> > :if=\>Proc.new{|u| u.value.is\_a?(Numeric)}
> 
> Try it as u.is\_a?(Numeric)

Thanks Litwin.  
I tried that before but it made the second validation stop working.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 10, 2010, 11:40pm UTC](https://www.ruby-forum.com/t/validation-problems/183213/4 "2010-02-10T23:40:43Z")

</div>

Sharagoz – wrote:

> > validates\_size\_of :value, :is=\>9, :message=\>“must be 5-digit number”,  
> > :if=\>Proc.new{|u| u.value.is\_a?(Numeric)}  
> > Looks corrent to me. Maybe the name of the field “value” is causing a  
> > conflict somewhere. Try to rename the field to something else just to  
> > test.  
> > Thanks Sharagoz. I tried different names for the field but none of  
> > those worked.  
> > I just wrote a custom validation to workaround it although still can’t  
> > figure it out.

Anyone has any idea about the second problem?

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 10, 2010, 11:52am UTC](https://www.ruby-forum.com/t/validation-problems/183213/5 "2010-02-10T11:52:51Z")

</div>

> validates\_size\_of :value, :is=\>9, :message=\>“must be 5-digit number”,  
> :if=\>Proc.new{|u| u.value.is\_a?(Numeric)}  
> Looks corrent to me. Maybe the name of the field “value” is causing a  
> conflict somewhere. Try to rename the field to something else just to  
> test.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 11, 2010, 12:56am UTC](https://www.ruby-forum.com/t/validation-problems/183213/6 "2010-02-11T00:56:51Z")

</div>

On Tue, Feb 9, 2010 at 4:03 PM, Ichiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, there.  
> I have two validations in the model:
> 
> validates\_numericality\_of :value, :only\_integer=\>true,  
> :allow\_blank=\>true  
> validates\_size\_of :value, :is=\>9, :message=\>“must be 5-digit number”,  
> :if=\>Proc.new{|u| u.value.is\_a?(Numeric)}

Ichiro, you have a bit of duplication within your validates\__. For  
example,  
you’re  
testing whether or not :value is numeric within both validates\__  
methods.  
Next, the  
:is compares the number of bytes and not the number of characters you  
should  
be  
able to do the following:

validates\_numericality\_of :value, :only\_integer =\> true, :allow\_blank  
=\>  
true # make sure that it’s an integer  
validate :number\_of\_digits  
# make sure that we have five digits

private

def number\_of\_digits  
unless self.value.to\_s.size == 5  
errors.add :value, “should be 5 digits”  
end  
end

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [February 11, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/validation-problems/183213/7 "2010-02-11T22:44:48Z")

</div>

> validates\_numericality\_of :value, :only\_integer =\> true, :allow\_blank  
> =\>  
> true # make sure that it’s an integer  
> validate :number\_of\_digits  
> # make sure that we have five digits
> 
> private
> 
> def number\_of\_digits  
> unless self.value.to\_s.size == 5  
> errors.add :value, “should be 5 digits”  
> end  
> end
> 
> Good luck,
> 
> -Conrad

Hi, Conrad. Thank you for the explanation. My workaround looks similar  
to your code and works. I still have no clue why :if=\>Proc.new{…}  
didn’t work though.
