条件回形针存在验证

时间:2013-08-26 12:57:28

标签: ruby-on-rails validation activerecord paperclip paperclip-validation

如果存在其他字段,如何验证回形针附件是否存在?我试过了:

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field
end

3 个答案:

答案 0 :(得分:2)

这里的类似问题,我的解决方案是在def

中进行比较
validate :check_image_with_title 

def check_image_with_title
   if !ctitle.blank? and cimage.blank?
      #If ctitle IS NOT empty and cimage IS empty, add a custom error message
      errors.add :key, "You need an image to go with your title"
      return false
    else
      return true
   end 
end

答案 1 :(得分:1)

试试这个: -

validates_attachment :img, presence: true, if: :some_other_field?
def some_other_field?
  some_other_field.present?
end

答案 2 :(得分:0)

您是否尝试使用exists?代替present?,可能有效

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field.exists?
end