SupremeA
SupremeA

Reputation: 1621

Validation of field with if option Mongoid

I have a model with validations and I want to make a validation that only is checked if another field in that same form is true (its a boolean). I am having trouble with the syntax of this validation. So far I have

class Reportapproval
  include Mongoid::Document

   field :manager_requested, type: Mongoid::Boolean, default: false
   field :disclosure_acceptance, type: Mongoid::Boolean, default: false

   validates_acceptance_of :disclosure_acceptance, if: :manager_requested == true, :accept => true

end

What is the proper syntax so that this validation is only checked if the manager_requested field is set to true.

P.S. Is it possible to check the manager_requested field if this is being created at the time of input.

Upvotes: 0

Views: 404

Answers (1)

kcdragon
kcdragon

Reputation: 1733

Try using a lambda or passing the method

validates_acceptance_of :disclosure_acceptance, if: lambda { manager_requested? }

or

validates_acceptance_of :disclosure_acceptance, if: :manager_requested?

Upvotes: 1

Related Questions