0

In my model file, I am trying to make a conditional validation depending on a hidden form value. It seems like the :form_type_main? method never gets called, or just does not work. Want am I doing wrong?

  attr_accessor(:form_type,:field1,:field2,:field3,:field4)

  required_main = ["field1", "field2"]
  required_second = ["field3", "field4"]

  if :form_type_main?
    required = required_main
  else
    required = required_second
  end

  required.each do |i|
    validates_presence_of i
  end


  def form_type_main?
    form_type == "main"
  end

1 Answer 1

1

You may simply define the validations like this:

REQUIRED_MAIN = [:field1, :field2]
REQUIRED_SECOND = [:field3, :field4]

validates_presence_of *REQUIRED_MAIN, if: :form_type_main?
validates_presence_of *REQUIRED_SECOND, unless: :form_type_main?

def form_type_main?
  form_type == "main"
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.