Created
September 5, 2011 01:49
-
-
Save pbyrne/1193878 to your computer and use it in GitHub Desktop.
Demonstrating Custom Validations to Prevent Specific Text
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
# Assumes a Comment model with a message attribute contianing the text of the | |
# comment. Change these to suit your needs. | |
class Comment | |
validate :disallow_phone_numbers_and_email_addresses | |
# possibly not the most performant, but it's clear | |
# assumes you have arrays of regexes you want to dissalow | |
def disallow_phone_numbers_and_email_addresses | |
if PHONE_NUMBERS.any? { |regex| message =~ regex } | |
errors.add(:message, "cannot contain phone numbers.") | |
end | |
if EMAIL_ADDRESSES.any? { |regex| message =~ regex } | |
errors.add(:message, "cannot contain email addresses.") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment