35 lines
885 B
Python
35 lines
885 B
Python
|
from django import forms
|
||
|
|
||
|
|
||
|
class MultiTextInput(forms.widgets.Input):
|
||
|
input_type = "text"
|
||
|
|
||
|
def render(self, name, value, attrs=None):
|
||
|
if value is not None:
|
||
|
selectedOptions = []
|
||
|
for val in value:
|
||
|
for k, v in self.choices:
|
||
|
if val == k:
|
||
|
selectedOptions.append(v)
|
||
|
|
||
|
value = " ".join(selectedOptions)
|
||
|
|
||
|
return super(MultiTextInput, self).render(name, value, attrs)
|
||
|
|
||
|
def value_from_datadict(self, data, files, name):
|
||
|
values = list(filter(bool, map(lambda _x: _x.strip(), data.get(name).split(" "))))
|
||
|
|
||
|
result = []
|
||
|
for value in values:
|
||
|
# FIXME: using value here throws a weird error message at some point
|
||
|
# could be handled by overriding the messages in ChoiceField
|
||
|
# or... well, don't know
|
||
|
kId = value
|
||
|
for k, v in self.choices:
|
||
|
if v.lower() == value.lower():
|
||
|
kId = str(k)
|
||
|
break
|
||
|
result.append(kId)
|
||
|
|
||
|
return result
|