Skip to content

Instantly share code, notes, and snippets.

@pbyrne
Created September 5, 2011 01:49
Show Gist options
  • Save pbyrne/1193878 to your computer and use it in GitHub Desktop.
Save pbyrne/1193878 to your computer and use it in GitHub Desktop.
Demonstrating Custom Validations to Prevent Specific Text
# 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