Class: Content::Validators::NameFormat

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/content/validators/name_format.rb

Constant Summary collapse

MIN_LENGTH =
1
MAX_LENGTH =
128

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate_length(record, attribute, value, max_length = MAX_LENGTH, min_length = MIN_LENGTH) ⇒ Object



32
33
34
35
36
37
# File 'app/models/content/validators/name_format.rb', line 32

def self.validate_length(record, attribute, value, max_length = MAX_LENGTH, min_length = MIN_LENGTH)
  if value
    record.errors[attribute] << _("cannot contain more than %s characters") % max_length unless value.length <= max_length
    record.errors[attribute] << _("must contain at least %s character") % min_length unless value.length >= min_length
  end
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/content/validators/name_format.rb', line 20

def validate_each(record, attribute, value)
  if value
    unless value =~ /\A[[:alnum:] _-]*\z/
      record.errors[attribute] << N_("cannot contain characters other than alpha numerals, space, '_', '-'")
    end
    NoTrailingSpace.validate_trailing_space(record, attribute, value)
    NameFormat.validate_length(record, attribute, value)
  else
    record.errors[attribute] << N_("cannot be blank")
  end
end