Hello all, Has anyone got some tips on validating files when uploaded? Validation in the model is a lot cleaner than in the controller and therefore I was hoping to retain this balance when uploading files. Has anyone found file upload validation as implemented in the model? --~--~---------~--~----~------------~-------~--~----~ 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 Dec 18, 6:42 am, "johnmcau...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <johnmcau...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello all, > > Has anyone got some tips on validating files when uploaded? > Validation in the model is a lot cleaner than in the controller and > therefore I was hoping to retain this balance when uploading files. > > Has anyone found file upload validation as implemented in the model?What kind of validations do you want to do? --~--~---------~--~----~------------~-------~--~----~ 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 response Chovy. Firstly there is a file uploaded and secondly to check that the file does not contain zero bites. I would like to keep everything in the model if possible. j On Dec 18, 5:49 pm, chovy <ettin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 18, 6:42 am, "johnmcau...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <johnmcau...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > Hello all, > > > Has anyone got some tips on validating files when uploaded? > > Validation in the model is a lot cleaner than in the controller and > > therefore I was hoping to retain this balance when uploading files. > > > Has anyone found file upload validation as implemented in the model? > > What kind of validations do you want to do?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Thanks for the response Chovy. > Firstly there is a file uploaded and secondly to check that the file > does not contain zero bites. > I would like to keep everything in the model if possible. > > jIt depends a little on how you are performing the uploads. If you are using attachment_fu, as I do, you can set the size range within the has_attachment call through the :size parameter, ex :size => 10..1000.bytes. You can also insert a custom validation expression within one of the validate methods, ex. def validate unless self.filename.blank? errors.add_to_base("Invalid file size") unless (10.kilobytes..10.megabytes).include?(self.size) end end At least these are a couple of ways 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 -~----------~----~----~----~------~----~------~--~---
On Dec 24, 10:23 pm, Chris Olsen <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> johnmcau...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Thanks for the response Chovy. > > Firstly there is a file uploaded and secondly to check that the file > > does not contain zero bites. > > I would like to keep everything in the model if possible. > > > j > > It depends a little on how you are performing the uploads. If you are > using attachment_fu, as I do, you can set the size range within the > has_attachment call through the :size parameter, ex :size => > 10..1000.bytes. You can also insert a custom validation expression > within one of the validate methods, ex. > > def validate > unless self.filename.blank? > errors.add_to_base("Invalid file size") unless > (10.kilobytes..10.megabytes).include?(self.size) > end > end > > At least these are a couple of ways to do it. > --I find that attachment_fu doesn''t give informative error messages, aside from "Picture is invalid". --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
chovy wrote:> On Dec 24, 10:23 pm, Chris Olsen <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> has_attachment call through the :size parameter, ex :size => >> At least these are a couple of ways to do it. >> -- > > > I find that attachment_fu doesn''t give informative error messages, > aside from "Picture is invalid".To be honest I too don''t like the way attachment_fu throws all the validation messages at once, which is why I like to put everything in the validate method and exclude the validates_as_attachment call. Then you have total control of what messages get shown to the users, although I have never seen a generic message like "Picture is invalid before". -- 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 -~----------~----~----~----~------~----~------~--~---