Validate or empty?

I want to do the following in a form:

a field may either empty or something must be following the validation
requirement.

if a user submit the form with the field empty, that’s fine. but if the
user submits something in the field, it must follow the validation
requirement.

for instance, an image field for a house for sale, if the user leaves
the image field empty, that means s/he has no picture, that could be
fine.

but, when s/he chooses submitting a pic, the pic file name must has
either .jpg, gif, or PNG ext.

I am using:

validates_format_of :image, :with=>/^.*(.jpg|.JPG|.gif|.GIF|.png|.PNG)$/

to validate the pic, but my problem is, when a user leaves image empty,
the validation still does not allow to save the record.

how may I fix it? thanks.

You need to add the :if argument like this:

validates_format_of :image, :with=>/^.*(.jpg|.JPG|.gif|.GIF|.png|.PNG)
$/,
:if => Proc.new { |obj| !obj.image.blank? }

You can also specify a method if you prefer:

validates_format_of :image, :with=>/^.*(.jpg|.JPG|.gif|.GIF|.png|.PNG)
$/,
:if => image_not_blank

private
def image_not_blank
!image.blank?
end

Aaron

for instance, an image field for a house for sale, if the user leaves
the image field empty, that means s/he has no picture, that could be
fine.
but, when s/he chooses submitting a pic, the pic file name must has
either .jpg, gif, or PNG ext.
I am using:
validates_format_of :image, :with=>/^.*
(.jpg|.JPG|.gif|.GIF|.png|.PNG)$/

You could try:

validates_format_of :image, :with=>/^($|.*
(.jpg|.JPG|.gif|.GIF|.png|.PNG)$)/

That looks for the beginning of the string, then either the immediate
end of the string, or some text ending in .jpg etc

As a side note, you might actually want this:

validates_format_of :image, :with=>/^($|.*(.jpe?g|.gif|.png)$)/i

For case-insensitive matching, in case they have foo.Jpg, and some
programs use .jpeg instead of .jpg, which this regex handles as well.

Hi,
Im using image_science package for uploading images images in my
application.

Can anybody tell me how to validate the format of images before
uploading images ?

This is my view

Upload Image <%= file_field 'image','uploaded_data' %>

and class is defined as this

class Image < ActiveRecord::Base

has_attachment :content_type => :image,
:storage => :file_system,
:size => 0.megabyte…2.megabytes,
:resize_to => ‘320x200>’,
:thumbnails => { :thumb => ‘50x50>’ },
:processor => ‘ImageScience’
validates_as_attachment
end

-Saurav

the above two ways really work!

Thanks. I have to learn more on regex. :wink:

When you say “validate the format”, what exactly are you trying to
validate? The size (width and height)? The image quality (72px/in)?

The browser won’t give you much information prior to actually uploading
the
files, so the answer is most likely no.

Not sure if I’ve helped much, but good luck with it.

On Tue, May 6, 2008 at 5:57 AM, Saurav C. <
[email protected]> wrote:

Upload Image :size => 0.megabyte..2.megabytes, >


James M.

Im trying to validate the format of image types, I want to upload image
of either jpg of png types only.
Is there any way around to do it ?

On May 7, 5:25 am, Saurav C. <rails-mailing-l…@andreas-
s.net> wrote:

Im trying to validate the format of image types, I want to upload image
of either jpg of png types only.
Is there any way around to do it ?

Hi Saurav,
The :content_type => :image option on your has_attachment line will
limit it to “all standard image types” (which I think consists
of .gif, .png and .jpg). You can override this by passing it an
explicit list of MIME types:

has_attachment :content_type => [‘image/png’, ‘image/jpeg’]

  • Matt

Well, if you want to validate the file name only, sure. Ultimately the
file
can be anything that the user names it, but here is how it’s done in the
depot app from the rails book:

validates_format_of :image_url,
                    :with     => %r{\.(gif|jpg|png)$}i,
                    :message  => "must be a URL for a GIF, JPG, or 

PNG
image"

Is that what you are looking for?

On Wed, May 7, 2008 at 12:25 AM, Saurav C. <
[email protected]> wrote:

Im trying to validate the format of image types, I want to upload image
of either jpg of png types only.
Is there any way around to do it ?


Posted via http://www.ruby-forum.com/.


James M.

Thanks for the info…its working now.

Matt Westcott wrote:

On May 7, 5:25 am, Saurav C. <rails-mailing-l…@andreas-
s.net> wrote:

Im trying to validate the format of image types, I want to upload image
of either jpg of png types only.
Is there any way around to do it ?

Hi Saurav,
The :content_type => :image option on your has_attachment line will
limit it to “all standard image types” (which I think consists
of .gif, .png and .jpg). You can override this by passing it an
explicit list of MIME types:

has_attachment :content_type => [‘image/png’, ‘image/jpeg’]

  • Matt

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs