Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:01:58+00:00 2026-05-26T10:01:58+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:01:59+00:00Added an answer on May 26, 2026 at 10:01 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate some user input that is encoded in UTF-8. Many have
Background I need to validate user input in some fields, where these are defining
I need to validate some user input, to ensure a number entered is in
In my ASP.net project I need to validate some basic data types for user
I need to validate an XML string (and not a file) against a DTD
I need to validate the user input of a JSpinner , and if invalid,
I need to validate two user input fields against each other in seam. Field1
I need to validate a phone number in javascript. The requirements are: they should
I need to validate a form. This is some server side validation that checks
I need to validate a model only for a certain action (:create). I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.