max_length.rb 652 B

123456789101112131415161718192021222324252627
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Validations::ObjectManager::AttributeValidator::MaxLength < Validations::ObjectManager::AttributeValidator::Backend
  3. def validate
  4. return if value.blank?
  5. return if irrelevant_attribute?
  6. validate_max_length
  7. end
  8. private
  9. def irrelevant_attribute?
  10. %w[input textarea].exclude? attribute.data_type
  11. end
  12. def maxlength
  13. attribute.data_option[:maxlength]
  14. end
  15. def validate_max_length
  16. return if value.length <= maxlength
  17. invalid_because_attribute(__('is longer than the allowed length %{maxlength}'), maxlength: maxlength)
  18. end
  19. end