Ticket #9212 (confirmed Bug)
SelectionWidget selects first value automatically
Reported by: | frisi | Owned by: | |
---|---|---|---|
Priority: | minor | Milestone: | 4.x |
Component: | Archetypes | Version: | 4.1 |
Keywords: | Cc: |
Description
When i define a StringField with an optional (required=False) value using a SelectionWidget i get two different behaviours depending on the type of selectionwidget i use::
atapi.StringField('floor', required = False, vocabulary = ['one','two','three'] widget = atapi.SelectionWidget( label = _(u"Floor"), type = 'radio', ), )
for type=='radio' i get five options, no option is chosen. submitting the form w/o touching the widget will result in context.floor == ::
[] one [] two [] three
for type=='select' i get a pulldown with 'one' pre-selected. submitting the form w/o touching this widget results in context.floor=='one'
To use a pulldown w/o having 'one' set by default i'd need to define
vocabulary = ['','one','two','three']
which would result in a useless selectable value in case we switch back to type=='radio'
Change History
comment:2 Changed 4 years ago by ida
to set the default-value to none and remove the empty string of the vocabulary would solve the problem, imho. voting to close this ticket.
comment:3 Changed 4 years ago by frisi
- Owner nouri deleted
this is still an issue with plone 4.1.
also, setting the default value to None does not help since the selectionwidget automatically sets the first value.
atapi.StringField('floor', required = False, default = None, vocabulary = ['one','two','three'] widget = atapi.SelectionWidget( label = _(u"Floor"), format = 'select', ), )
the selsctionwidget should automatically add an empty item to the vocabulary in case it's format is set to 'select'
comment:4 follow-up: ↓ 6 Changed 4 years ago by ida
agree in general but there's a pitfall, imagine this usecase:
you have a contype employee which has a selection-field for the department the person works for.
department 'A' closes down, we remove 'A' of the list, so it's not selectable anymore.
then you edit the entry of a retired person who has worked for 'A', to change f.e. the phonenumber-field.
the selectionfield then will not display the formerly saved value 'A' anymore because it's not in the list and falls back to the default value, which is not none but an empty string.
this would overwrite the department-value unintentionally and thereby corrupt the record.
comment:6 in reply to: ↑ 4 Changed 4 years ago by frisi
Replying to ida:
agree in general but there's a pitfall, imagine this usecase:
you have a contype employee which has a selection-field for the department the person works for. department 'A' closes down, we remove 'A' of the list, so it's not selectable anymore. then you edit the entry of a retired person who has worked for 'A', to change f.e. the phonenumber-field.
the selectionfield then will not display the formerly saved value 'A' anymore because it's not in the list and falls back to the default value, which is not none but an empty string.
this would overwrite the department-value unintentionally and thereby corrupt the record.
i'm quite sure this usecase is not supported by the current implementation either.
also, this ticket is not about how to deal with altering vocabularies for existing objects.
you came up with the proposal with default value and i wanted to show that this does not solve the problem.
the widget simply should be able to handle widget.format='select' in combination with field.required=False
Still an issue in Plone 4?