Skip to content

Passing on validators to select subfields seems wrong #715

@ThiefMaster

Description

@ThiefMaster

This was added in #615 but it breaks custom select field subclasses that use e.g. a list of checkboxes as they now all get the required attribute.

Here's a failing test case (since both inputs get the required flag):

def test_required_flag_custom():
    class CustomWidget:
        def __call__(self, field, **kwargs):
            html = []
            for subfield in field:
                html.append(f'{subfield.label}: {subfield()}')
            return ''.join(html)

    class CustomSelectMultipleField(SelectMultipleField):
        widget = CustomWidget()
        option_widget = CheckboxInput()

    F = make_form(
        c=CustomSelectMultipleField(
            choices=[("a", "hello"), ("b", "bye")],
            validators=[validators.DataRequired()],
        )
    )
    form = F(DummyPostData(c=["a"]))
    assert form.c() == (
        '<label for="c-0">hello</label>: <input checked id="c-0" name="c" type="checkbox" value="a">'
        '<label for="c-1">bye</label>: <input id="c-1" name="c" type="checkbox" value="b">'
    )

I have the feeling that passing validators to subfields isn't the correct thing to do - only <input type="radio"> handles the required flag correctly, but of course for multi-select you'll never have radio buttons...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions