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 8613229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:49:21+00:00 2026-06-12T04:49:21+00:00

I have a form with 3 ActiveRecord fields. One of those fields has kind

  • 0

I have a form with 3 ActiveRecord fields. One of those fields has kind of goofy, and STATE-DEPENDENT validation requirements. (For example, I only validate the field if the object is being created on a setup wizard form.)

In my POST handler to create the object, I thought I could call errors.add to insert a special error condition

@foo = Foo.new(params[:foo])
if goofy_conditions(params[:foo][:goofy_field])
  @foo.errors.add(:goofy_field, "doesn't meet the goofy conditions" )
end
respond_to do |format|
  if @foo.save
    ...
  else
    ... redirect back to form (with error fields hilited)

However, doing @foo.errors.add() in the controller doesn’t seem to do anything… it doesnt prevent the save() if the other fields pass validations.

An alternative is to put a custom validation handler into the model… I know using errors.add(:field, ‘msg’) works fine there… but in that case how can my controller ‘pass’ info to the validator telling it whether or not the field needs to be validated.

  • 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-06-12T04:49:23+00:00Added an answer on June 12, 2026 at 4:49 am

    That is model logic. Look at custom validations

    class GoofyThing < ActiveRecord::Base
      validate :goofy_attribute_is_goofy
    
      def goofy_attribute_is_goofy
        if goofy_conditions(self.goofy_field)
          self.errors.add(:goofy_field, "doesn't meet the goofy conditions" )
        end
      end
    end
    

    Then it’ll act just like any other validation.

    Edit

    You can conditionally validate with the :if option:

    attr_accessible :via_wizard
    validate :goofy_attribute_is_goofy, :if => lambda { self.via_wizard }
    

    and in your controller:

    class WizardController < ApplicationController
      before_filter :get_object, :set_wizard
    
      #...
    
      def get_object
        @object = GoofyThing.find(params[:id])
      end
    
      def set_wizard
        @object.via_wizard = true
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

each User can only have one MedicalHistory so I have class User < ActiveRecord::Base
I have form where are 2 input fields and then i have dynamic dataTables
I have form, where some fields are looks like rows, so I can add/delete
I have one model but two different forms ,one form i am saving through
I have an invoice model, that has many invoice items. I have a form
I have a Content model which has one or many Audio files which need
I have three models presented in a multi-model form. class Parent < ActiveRecord::Base has_many
I have a model class 'Market' which has many products: class Market < ActiveRecord::Base
I have two models with a one-to-one association. class User < ActiveRecord::Base has_one :setting
I have a sign-up form that has nested associations/attributes whatever you want to call

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.