views:

1106

answers:

2

I have a Product model which validates multiple attributes (including a Paperclip image attachment) like so:

validates_presence_of :name
validates_format_of :name, :with => /^([a-zA-Z0-9\ \-]{3,128})$/i
...
has_attached_file :image

validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ["image/jpeg", "image/png", "image/gif"]

Everything is working fine. What I want now is to make an (unobtrusive) hidden iframe in-place upload script using javascript. My problem is that I cannot just upload the image without the rest of the data, because it will fail validation (no name present) and also I cannot send the rest of the form without the image (same thing, fails validation).

So basically what I need (and don't know how to achieve) is to conditionally apply the model validations according to what the action is currently in progress (uploading the image or editing other data).

I hope I was clear enough. Any help is appreciated. Thanks.

+1  A: 

Railscasts have a nice video screencast about conditional validations.

kyku
now, I wonder how I didn't find that, because I *did* say "conditional validation". I'd give it a look and get back to you. Thanks!
andi
It was just what I needed. Thanks.
andi
+1  A: 

I hate having to wade through a video just to get a simple answer. In fact, I think that a blog entry is superior to a simple tutorial video simply for the fact that it is searchable. Here is a simple case in plain text for anyone else looking:

To validate the presence of password only for the create action, do this:

validates_presence_of :password, :on => create
Peter D.