Sha256: af32e64a73a67745454dabbf2d76674c3e1bb3e94f2b012ee55deeee17c31393

Contents?: true

Size: 738 Bytes

Versions: 2

Compression:

Stored size: 738 Bytes

Contents

class AlphaValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('errors.messages.alpha'))
    end
  end

  private

  def valid_format?(value, options)
    case options[:case]
    when :lower
      options[:strict] ? (value =~ /^[a-z]+$/) : (value =~ /^[a-z ]+$/)
    when :upper
      options[:strict] ? (value =~ /^[A-Z]+$/) : (value =~ /^[A-Z ]+$/)
    else
      options[:strict] ? (value =~ /^[A-Za-z]+$/i) : (value =~ /^[A-Za-z ]+$/i)
    end
  end

  def valid_length?(value)
    value != ' '
  end

  def valid?(value, options)
    valid_format?(value, options) &&
    valid_length?(value)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flash_validators-3.0.1 lib/flash_validators/validators/alpha_validator.rb
flash_validators-3.0.0 lib/flash_validators/validators/alpha_validator.rb