Validate attribute only if it present (only if user fill in it) Validate attribute only if it present (only if user fill in it) ruby ruby

Validate attribute only if it present (only if user fill in it)


Add :allow_blank => true and it should do what you want.


Maybe :if => lambda {|attr| attr.present?} will help?


Some validations accept the options :allow_blank => true or :allow_nil => true.

If this fails, use :if condition, like this:

validates_format_of :avatar_file_name,  :with=>/\.(jpeg|jpg|png|gif)$/i,  :message=> I18n.t("..."),  :on => :update, :if => lambda{ |object| object.avatar_file_name.present? }

But i encourage you to use allows. Much cleaner.