Skip to content

Bug with multi select fields #122

@schatimo

Description

@schatimo

There is a bug with multi select fields when only one item is selected leading to an incorrect 422 error.

E.g., when selecting only one item for the "Select Multiple" select element on https://fastui-demo.onrender.com/forms/select, the POST request throws an unexpected 422 error:

{
    "detail": {
        "form": [
            {
                "type": "list_type",
                "loc": [
                    "select_multiple"
                ],
                "msg": "Input should be a valid list"
            }
        ]
    }
}

This might be related to fastapi/fastapi#8741. A work-around, not elegant though, would be to add the following validation to SelectForm (https://github.com/pydantic/FastUI/blob/main/demo/forms.py):

@field_validator('select_multiple', mode='before')
@classmethod
    def correct_list_fields(cls, value: List[str] | str) -> List[str]:
        if isinstance(value, list):
            return value
        else:
            return [value]

Any thoughts on a neater solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions