Created
September 10, 2015 10:15
-
-
Save kikito/c3da655d20cd0c0bbef2 to your computer and use it in GitHub Desktop.
Comment length validation options
This file contains 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
class Comment < ActiveRecord::Base | |
validate do | |
validator = ActiveModel::Validations::LengthValidator.new( | |
attributes: :body, | |
minimum: 10, | |
maximum: Comment.body_max_length) | |
validator.validate(self) | |
end | |
def self.body_max_length | |
1000 | |
end | |
end | |
This file contains 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
class Comment < ActiveRecord::Base | |
validate :validate_body_length | |
def self.body_max_length | |
1000 | |
end | |
private | |
def validate_body_length | |
validator = ActiveModel::Validations::LengthValidator.new( | |
attributes: :body, | |
minimum: 10, | |
maximum: Comment.body_max_length) | |
validator.validate(self) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment