Fixes #2973 - saving Boolean field with empty values.

This commit is contained in:
Rolf Schmidt 2021-07-19 17:02:34 +00:00 committed by Thorsten Eckel
parent b3da6d31ff
commit 5675887ebe
2 changed files with 67 additions and 0 deletions

View file

@ -39,6 +39,21 @@ treeParams = (e, params) ->
params.data_option.options = tree params.data_option.options = tree
params params
setSelectDefaults = (el) ->
data_type = el.find('select[name=data_type]').val()
return if data_type isnt 'select' && data_type isnt 'boolean'
el.find('.js-value, .js-valueTrue, .js-valueFalse').each(->
element = $(@)
return true if element.val()
if element.hasClass('js-valueTrue') || element.hasClass('js-valueFalse')
element.val(element.attr('placeholder'))
else
key_value = element.closest('tr').find('.js-key').val()
element.val(key_value)
)
class ObjectManager extends App.ControllerTabs class ObjectManager extends App.ControllerTabs
requiredPermission: 'admin.object' requiredPermission: 'admin.object'
constructor: -> constructor: ->
@ -179,6 +194,8 @@ class Items extends App.ControllerSubContent
class New extends App.ControllerGenericNew class New extends App.ControllerGenericNew
onSubmit: (e) => onSubmit: (e) =>
setSelectDefaults(@el)
params = @formParam(e.target) params = @formParam(e.target)
params = treeParams(e, params) params = treeParams(e, params)
@ -242,6 +259,8 @@ class Edit extends App.ControllerGenericEdit
@controller.form @controller.form
onSubmit: (e) => onSubmit: (e) =>
setSelectDefaults(@el)
params = @formParam(e.target) params = @formParam(e.target)
params = treeParams(e, params) params = treeParams(e, params)

View file

@ -65,4 +65,52 @@ RSpec.describe 'Admin Panel > Objects', type: :system, authenticated_as: true do
expect(ObjectManager::Attribute.last.data_option).to eq(expected_data_options) expect(ObjectManager::Attribute.last.data_option).to eq(expected_data_options)
end end
it 'checks smart defaults for select field' do
page.find('.js-new').click
fill_in 'Name', with: 'select1'
find('input[name=display]').set('select1')
page.find('select[name=data_type]').select('Select')
page.first('div.js-add').click
page.first('div.js-add').click
page.first('div.js-add').click
counter = 0
page.all('.js-key').each do |field|
field.set(counter)
counter += 1
end
page.all('.js-value')[-2].set('special 2')
page.find('.js-submit').click
expected_data_options = {
'0' => '0',
'1' => '1',
'2' => 'special 2',
}
expect(ObjectManager::Attribute.last.data_option['options']).to eq(expected_data_options)
end
it 'checks smart defaults for boolean field' do
page.find('.js-new').click
fill_in 'Name', with: 'bool1'
find('input[name=display]').set('bool1')
page.find('select[name=data_type]').select('Boolean')
page.find('.js-valueFalse').set('HELL NOO')
page.find('.js-submit').click
expected_data_options = {
true => 'yes',
false => 'HELL NOO',
}
expect(ObjectManager::Attribute.last.data_option['options']).to eq(expected_data_options)
end
end end