# Validates\_file\_format\_of only when is there an image

**URL:** <https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290>\
**Category:** Rails\
**Created:** [December 27, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290 "2007-12-27T12:23:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [December 27, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290/1 "2007-12-27T12:23:30Z")

</div>

i have a model with this:  
file\_column :image  
validates\_file\_format\_of :image, :in =\> [“gif”, “png”, “jpg”]

If inthe form the user not insert the image i receive an error.  
If i remove:  
validates\_file\_format\_of :image, :in =\> [“gif”, “png”, “jpg”]  
it works.

Is possible do the validation only when user insert the image in form?

i try with:  
validates\_file\_format\_of :image, :in =\> [“gif”, “png”, “jpg”],:if  
=\>:image?  
but it not works.

How i can do?

Thanks

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [December 27, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290/2 "2007-12-27T16:36:40Z")

</div>

Luca R. wrote:

> Is possible do the validation only when user insert the image in form?
> 
> i try with:  
> validates\_file\_format\_of :image, :in =\> [“gif”, “png”, “jpg”],:if  
> =\>:image?  
> but it not works.

The option

:allow\_nil =\> true

should do the trick.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [December 27, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290/3 "2007-12-27T18:44:22Z")

</div>

Didn’t seem to work for me.

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [December 28, 2007, 11:44am UTC](https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290/4 "2007-12-28T11:44:59Z")

</div>

chovy wrote:

> > :allow\_nil =\> true
> > 
> > should do the trick.

That’s unexpected. The image attribute should be nil if nothing  
has been uploaded, and the validation should be skipped because  
the :allow\_nil is passed into the validates\_each that’s used by  
validates\_file\_format\_of.

You could try :allow\_blank =\> true instead. But what’s been  
working for me is a modified file\_column that puts an  
“unless value.blank?” inside the validates\_each. I think I did  
this before validates\_each supported the allow\_blank option.

So perhaps rather than :allow\_nil, :allow\_blank is needed,  
although I’m not sure why because the no-image condition is  
a nil attribute, not an empty string.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [December 28, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/validates-file-format-of-only-when-is-there-an-image/124290/5 "2007-12-28T21:04:02Z")

</div>

I’m actually using this with validates\_format\_of (neither allow\_blank  
or allow\_nil work).
