ruby on rails - Get validation errors only on #valid? -
i have model field validating content in field. using activerecord validations check presence of content. however, want able save, update, etc, without checking validity. want validity @ 1 specific time, , retrieve errors it.
validates :my_content_in_field, presence: true, if: :should_validate attr_accessor :should_validate
i want pass
valid?
and fail
valid?(should_validate: true)
and after failed validation want updates , saves work per usual. possible? want leverage activerecords error messages, not validate otherwise.
at end of day on friday, may missing obvious.
i'm not sure can call valid?(should_validate: true)
. valid?
method may called 1 parameter called context
(see docs). should read a great post on validation contexts.
this should work:
class mymodel < activerecord::base validates :something, on: :should_validate ... end m = mymodel.new m.valid? # no validation happens m.valid?(:should_validate) # validates
Comments
Post a Comment