Ticket #9212 (confirmed Bug)

Opened 7 years ago

Last modified 4 years ago

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'

( discussion on plone.users)

Change History

comment:1 Changed 4 years ago by kleist

  • Status changed from new to confirmed
  • Version set to 4.1

Still an issue in Plone 4?

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.

Last edited 4 years ago by ida (previous) (diff)

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.

Last edited 4 years ago by ida (previous) (diff)

comment:5 Changed 4 years ago by hannosch

  • Milestone changed from 3.3.x to 4.x

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

Note: See TracTickets for help on using tickets.