Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The ScoredAnswerWidget used in review forms breaks forms if set to required #4179

Open
frjo opened this issue Oct 23, 2024 · 0 comments
Open
Labels
Type: Bug Bugs! Things that are broken :-/

Comments

@frjo
Copy link
Contributor

frjo commented Oct 23, 2024

This is a long standing bug I believe. The textarea gets set to "required" even when hidden by the TinyMCE editor.

The rich text field does not have this issue. So it is the implementation in ScoredAnswerWidget that likely is at fault.

[Error] An invalid form control with name='71bfe95d-89c5-401b-ae7a-778e91d5c8c5_0' is not focusable.

class ScoredAnswerWidget(forms.MultiWidget):
def __init__(self, attrs=None):
_widgets = (
TinyMCE(attrs=attrs, mce_attrs=MCE_ATTRIBUTES_SHORT),
widgets.Select(attrs=attrs, choices=RATE_CHOICES),
)
super().__init__(_widgets, attrs)
def decompress(self, value):
# We should only hit this on initialisation where we set the default to a list of None
if value:
return value
return [None, None]
def render(self, name, value, attrs=None, renderer=None):
context = self.get_context(name, value, attrs)
rendered = []
# We need to explicitly call the render method on the tinymce widget
# MultiValueWidget just passes all the context into the template
for kwargs, widget in zip(
context["widget"]["subwidgets"], self.widgets, strict=False
):
name = kwargs["name"]
value = kwargs["value"]
attrs = kwargs["attrs"]
rendered.append(widget.render(name, value, attrs, renderer))
return mark_safe("".join(list(rendered)))
class ScoredAnswerField(forms.MultiValueField):
widget = ScoredAnswerWidget
def __init__(self, *args, **kwargs):
fields = (
forms.CharField(),
forms.ChoiceField(choices=RATE_CHOICES),
)
super().__init__(*args, **kwargs, fields=fields)
def compress(self, data_list):
if data_list:
return [data_list[0], int(data_list[1])]
else:
return ["", NA]

@frjo frjo added the Type: Bug Bugs! Things that are broken :-/ label Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bugs! Things that are broken :-/
Projects
None yet
Development

No branches or pull requests

1 participant