Created
July 31, 2018 00:26
-
-
Save leandromarques/31889a66e0cbd7db81cde1d5ebe13cb3 to your computer and use it in GitHub Desktop.
Conditional validation example in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
validates :datetime_is_not_in_past, if: :datetime_not_nil? | |
def datetime_is_not_in_past | |
errors.add(:datetime_field_name, :invalid, message: "cannot be in the past") if self.datetime_field_name < DateTime.current | |
end | |
def datetime_not_nil? | |
!self.datetime_field_name.nil? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment