Skip to content

Add class_name option for Models used in ModelFormΒ #353

@ibe-314

Description

@ibe-314

Currently when using ModelForm every form field is put into a new row. Here is an example:

grafik

The form above is generated using FastUIs ModelForm with the following pydantic Model:

class ExampleModel(BaseModel):
    first_name: Optional[str] = Field(
        None, title='First Name'
    )
    family_name: Optional[str] = Field(
        None, title='Family Name'
    )
    birth_date: Optional[date] = Field(None, title='Birth Date')
    remark: Optional[str] = Field(None, title='Remark')
    info: Annotated[Optional[str], Textarea(rows=5)] = Field(None, description='Optional free text information about you.')
    repo: str = Field(json_schema_extra={'placeholder': '{org}/{repo}'}, title='GitHub repository')

I would like to allign mutliple fields in one row using Bootstraps row and column functionality. I was not able to do this using ModelForm, however I found a solution using the Form class from fastui.components.forms:

manuel_model = c.Form(
        form_fields=[
            FormFieldInput(
                name="first_name",
                title="First Name",
                class_name="+ col-sm-4",
            ),
            FormFieldInput(
                name="family_name",
                title="Family Name",
                class_name="+ col-sm-4",
            ),
            FormFieldInput(
                name="birth_date",
                title="Birth Date",
                html_type="date",
                class_name="+ col-sm-4",
            ),
            FormFieldInput(
                name="remark",
                title="Remark",
                class_name="+ col-sm-6",
            ),
            FormFieldTextarea(
                name="info",
                title="Info",
                class_name="+ col-sm-6",
            ),
            FormFieldInput(
                name="repo",
                title="GitHub repository",
                class_name="+ col-sm-12",
            )
        ],
        submit_url="/api/login",
        class_name="+ row",
    )

which results in

grafik

I looked into the code of ModelForm, where the model_json_schema_to_fields function renders the Pydantic Model. Is there currently some possiility to add class_name attributes to the ModelForm fields? Or some other possibility to get the second layout using the ModelForm class?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions