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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
the above two ways really work! Thanks. I have to learn more on regex. ;) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <TD HEIGHT=24>Upload Image </TD> <TD HEIGHT=24 ><%= file_field ''image'',''uploaded_data'' %></TD> </TR> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Chakraborty < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > > <TD HEIGHT=24>Upload Image </TD> > <TD HEIGHT=24 ><%= file_field ''image'',''uploaded_data'' > %></TD> > </TR> > > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >-- James Mitchell --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Chakraborty < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Mitchell --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On May 7, 5:25 am, Saurav Chakraborty <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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the info...its working now. Matt Westcott wrote:> On May 7, 5:25 am, Saurav Chakraborty <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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---