Class: Emoji::Validator::NoEmojiValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/emoji/validator/no_emoji_validator.rb

Overview

Validate an attribute against emojis

class Person < ApplicationRecord
  validates :first_name, no_emoji: true
end

person = Person.new(first_name: "John", last_name: "😃")
person.valid? #true
person.first_name = "😃"
person.valid? #false

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



18
19
20
21
22
23
# File 'lib/emoji/validator/no_emoji_validator.rb', line 18

def validate_each(record, attribute, value)
  return if value.nil?
  return if value.match(Unicode::Emoji::REGEX_VALID).nil?

  record.errors.add(attribute, :has_emojis)
end