mirror of
https://github.com/zammad/zammad
synced 2026-05-24 09:48:36 +00:00
Fixes #2150 - Text attribute inputs longer than maxlength is silently truncated
This commit is contained in:
parent
b4bccce969
commit
bc66012281
2 changed files with 89 additions and 0 deletions
27
app/models/object_manager/attribute/validation/max_length.rb
Normal file
27
app/models/object_manager/attribute/validation/max_length.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
||||
|
||||
class ObjectManager::Attribute::Validation::MaxLength < ObjectManager::Attribute::Validation::Backend
|
||||
|
||||
def validate
|
||||
return if value.blank?
|
||||
return if irrelevant_attribute?
|
||||
|
||||
validate_max_length
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def irrelevant_attribute?
|
||||
%w[input textarea].exclude? attribute.data_type
|
||||
end
|
||||
|
||||
def maxlength
|
||||
attribute.data_option[:maxlength]
|
||||
end
|
||||
|
||||
def validate_max_length
|
||||
return if value.length <= maxlength
|
||||
|
||||
invalid_because_attribute("is longer than the allowed length #{maxlength}")
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
||||
|
||||
require 'rails_helper'
|
||||
require 'models/object_manager/attribute/validation/backend_examples'
|
||||
|
||||
RSpec.describe ::ObjectManager::Attribute::Validation::MaxLength do
|
||||
|
||||
subject do
|
||||
described_class.new(
|
||||
record: record,
|
||||
attribute: attribute
|
||||
)
|
||||
end
|
||||
|
||||
let(:record) { build(:user) }
|
||||
|
||||
%w[text textarea].each do |suffix|
|
||||
let(:attribute) { build("object_manager_attribute_#{suffix}") }
|
||||
|
||||
context "with #{suffix} type" do
|
||||
context 'when validation should not be performed' do
|
||||
describe 'for blank value' do
|
||||
let(:value) { nil }
|
||||
|
||||
it_behaves_like 'a validation without errors'
|
||||
end
|
||||
|
||||
describe 'for irrelevant attribute data_type' do
|
||||
let(:value) { 'some_value' }
|
||||
|
||||
before { attribute.data_type = 'select' }
|
||||
|
||||
it_behaves_like 'a validation without errors'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when validation should be performed' do
|
||||
describe "for data_option 'maxlength'" do
|
||||
before { attribute.data_option[:maxlength] = 10 }
|
||||
|
||||
context 'when value is the same as data_option' do
|
||||
let(:value) { 'test valūe' }
|
||||
|
||||
it_behaves_like 'a validation without errors'
|
||||
end
|
||||
|
||||
context 'when value is valid' do
|
||||
let(:value) { 'test' }
|
||||
|
||||
it_behaves_like 'a validation without errors'
|
||||
end
|
||||
|
||||
context 'when value is invalid' do
|
||||
let(:value) { 'long text here' }
|
||||
|
||||
it_behaves_like 'a validation with errors'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue