fix: #187 provide eav value during field cleaning

This commit is contained in:
Mike 2022-06-12 21:58:44 -07:00
parent d643bddb95
commit 8332a39def
2 changed files with 22 additions and 3 deletions

View file

@ -32,6 +32,7 @@ class CSVFormField(forms.Field):
def __init__(self, *args, **kwargs):
kwargs.pop('max_length', None)
self.separator = kwargs.pop('separator', self.default_separator)
super().__init__(*args, **kwargs)
def to_python(self, value):
@ -39,10 +40,10 @@ class CSVFormField(forms.Field):
return []
return [v.strip() for v in value.split(self.separator) if v]
def validate(self, value):
super().validate(value)
def validate(self, field_value):
super().validate(field_value)
try:
isinstance(value.split(self.separator), list)
isinstance(field_value, list)
except ValidationError:
raise ValidationError(self.message, code=self.code)

View file

@ -21,3 +21,21 @@ class CSVWidget(Textarea):
def render(self, name, value, **kwargs):
value = self.prep_value(value)
return super().render(name, value, **kwargs)
def value_from_datadict(self, data, files, name):
"""
Return the value of this widget or None.
Since we're only given the value of the entity name and the data dict
contains the '_eav_config_cls' (which we don't have access to) as the
key, we need to loop through each field checking if the eav attribute
exists with the given 'name'.
"""
widget_value = None
for data_value in data:
try:
widget_value = getattr(data.get(data_value), name)
except AttributeError:
pass # noqa: WPS420
return widget_value