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?
Add :allow_blank => true
and it should do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With