validates_presence_of if condition on rails 3.2 an

2020-07-18 05:06发布

问题:

I want validate presence of these 2 attributes :shipping_cost and :shipping_cost_anywhere if the attribute :shipping is equal to true. and If

I have this in my model but not working fine for me:

validates_presence_of :shipping_cost, :shipping_cost_anywhere, :allow_blank => "true" if :shipping == "true"

this is my :shipping attribute:

field :shipping, :type => Boolean, :default => "false"

How can I do it?

Thank you!

Edited.

I'm using mongoid and simple_form gems

回答1:

validates_presence_of :shipping_costs_anywhere, :if => :should_be_filled_in?

def should_be_filled_in?
  shipping_costs_anywhere == "value"
end

The method will return true or false when it's called in the statement. No need to put colon in front of shipping_costs_anywhere.



回答2:

The fix for me to this question is the next code:

validates :shipping_cost, :shipping_cost_anywhere, :presence => true, :if => :shipping?

Thank you to all for your help but any answer has worked for me. thanks!



回答3:

Stumbled across this today and thought I'd freshen the answer. As others mentioned, you can put the logic in a function. However, you can also just throw it in a proc.

validates_presence_of :shipping_costs_anywhere, :if => Proc.new { |o|
  o.shipping_costs_anywhere == "value"}

http://guides.rubyonrails.org/active_record_validations.html#using-a-symbol-with-if-and-unless



回答4:

The validates is now preferred over validates_presences_of etc. As hyperjas mentioned you can do this:

validates :shipping_cost,
  :shipping_cost_anywhere,
  :presence => true, :if => :shipping?

However, that conditionalizes the entire validation for both :shipping_cost and :shipping_cost_anywhere. For better maintainability, I prefer a separate validate declaration for each attribute.

More importantly, you will likely run into situations where you have multiple validations with different conditions (like one for presence and another for length, format or value). You can do that like this:

validates :shipping_cost,
  presence: { if: :shipping? },
  numericality: { greater_than: 100, if: :heavy? }

You can also let rails evaluate a ruby string.

validates :shipping_cost,
  presence: { if: "shipping?" },
  numericality: { greater_than: 100, if: "shipping? and heavy?" }

And finally, optionally add separate custom messages:

validates :shipping_cost,
  presence: { if: "shipping?", message: 'You forgot the shipping cost.' },
  numericality: { greater_than: 100, if: "shipping? and heavy?", message: 'Shipping heavy items is $100 minimum.' }

And so on. Hope that helps.



回答5:

I can't test it, but I think the syntax is more like:

validates_presence_of :shipping_cost, :shipping_cost_anywhere, :allow_blank => "true", :if => "shipping.nil?"

See:

http://guides.rubyonrails.org/active_record_validations_callbacks.html#conditional-validation



回答6:

Here is my code working for me.Call method on if condition rather than comparing

 validates :prefix, :allow_blank => true, :uniqueness => { :case_sensitive => true } ,:if => :trunk_group_is_originating?


        def trunk_group_is_originating?
          if self.direction == "originating"
            true
          else
            false
          end
        end

Hope it helps you.......