Rails - Validation :if one condition is true
You can pass a lambda to be evaluated as the if
condition.
Try:
validates :description, presence: true, if: -> { first_step? || require_validation }
If you don't want to add one method as Jared say then you can try use lambda
validates :description, presence: true, if: ->{ first_step? || require_validation }
Can you just wrap it in one method? According to the docs
:if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. if: :allow_validation, or if: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
validates :description, presence: true, if: :some_validation_check
def some_validation_check
first_step? || require_validation
end
You can use a lambda for the if:
clause and do an or condition.
validates :description, presence: true, if: -> {current_step == steps.first || require_validation}