class UsernameValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless value =~ /^[a-z0-9_-]{2,16}$/ record.errors[attribute] << (options[:message] || I18n.t('flash_validators.errors.messages.username')) end end end