forms: add support for html attributes on single select widget options (#48549)
This commit is contained in:
parent
be68194bc8
commit
5db7c5fff8
|
@ -58,7 +58,7 @@ import quixote.form.widget
|
|||
|
||||
from quixote import get_publisher, get_request, get_response, get_session
|
||||
from quixote.form import *
|
||||
from quixote.html import htmltext, htmltag, htmlescape, TemplateIO
|
||||
from quixote.html import htmltext, htmltag, htmlescape, TemplateIO, stringify
|
||||
from quixote.http_request import Upload
|
||||
from quixote.util import randbytes
|
||||
|
||||
|
@ -958,6 +958,44 @@ class EmailWidget(StringWidget):
|
|||
self.error = _('invalid address domain')
|
||||
|
||||
|
||||
class SingleSelectWidget(quixote.form.widget.SingleSelectWidget):
|
||||
def set_options(self, options, *args, **kwargs):
|
||||
# options can be,
|
||||
# - [objects:any, ...]
|
||||
# - [(object:any, description:any), ...]
|
||||
# - [(object:any, description:any, key:any), ...]
|
||||
# - [(object:any, description:any, key:any, html_attrs:dict), ...]
|
||||
self.options = [] # for compatibility with existing quixote methods
|
||||
self.full_options = []
|
||||
for option in options:
|
||||
if isinstance(option, (tuple, list)):
|
||||
if len(option) == 2:
|
||||
option_tuple = (option[0], option[1], stringify(option[1]))
|
||||
elif len(option) == 3:
|
||||
option_tuple = (option[0], option[1], stringify(option[2]))
|
||||
elif len(option) >= 4:
|
||||
option_tuple = (option[0], option[1], stringify(option[2]), option[3])
|
||||
else:
|
||||
option_tuple = (option, option, stringify(option))
|
||||
self.full_options.append(option_tuple)
|
||||
self.full_options = [x + ({},) if len(x) == 3 else x for x in self.full_options]
|
||||
self.options = [x[:3] for x in self.full_options]
|
||||
|
||||
def render_content(self):
|
||||
tags = [htmltag("select", name=self.name, **self.attrs)]
|
||||
for object, description, key, attrs in self.full_options:
|
||||
if self.is_selected(object):
|
||||
selected = 'selected'
|
||||
else:
|
||||
selected = None
|
||||
if description is None:
|
||||
description = ""
|
||||
r = htmltag("option", value=key, selected=selected, **attrs)
|
||||
tags.append(r + htmlescape(description) + htmltext('</option>'))
|
||||
tags.append(htmltext("</select>"))
|
||||
return htmltext("\n").join(tags)
|
||||
|
||||
|
||||
class ValidationCondition(Condition):
|
||||
def __init__(self, django_condition, value):
|
||||
super(ValidationCondition, self).__init__({'type': 'django', 'value': django_condition})
|
||||
|
|
Loading…
Reference in New Issue