Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need validate some attributes ONLY if they are not empty.

For example the user may have a logo. If we try to load it - validation should work. If we simply update the user's data without the logo, validation must be skipped.

Now i have:

The form has a choice of two files. One - logo, second - avatar. Both of this attributes is part of User model. In User model a have validation:

validates_preference_of :logo_file_name, :message=>I18n.t("...") validates_format_of :logo_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("...") validates_preference_of :avatar_file_name, :message=>I18n.t("...") validates_format_of :avatar_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("...") 

In this case, if we try to create a new User without selected logo and avatar, we will have errors (our validation). I tryed change validation and add ":on => :update" like this:

validates_preference_of :logo_file_name, :message=>I18n.t("..."), :on => :update validates_format_of :logo_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("..."), :on => :update validates_preference_of :avatar_file_name, :message=>I18n.t("..."), :on => :update validates_format_of :avatar_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("..."), :on => :update 

Now i can create user without selected logo and avatar, but if i try edit user and try upload only logo - i have validation errors of avatar. If i choose file for avatar and logo leave blank - i have validation errors for logo.

How i can run validation ony for attribute that I want to change?

like image 301
prosto.vint Avatar asked Oct 23 '11 18:10

prosto.vint


1 Answers

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

like image 159
Mark Thomas Avatar answered Oct 12 '22 22:10

Mark Thomas