iommirocks / iommi

Your first pick for a django power cord
http://iommi.rocks
BSD 3-Clause "New" or "Revised" License
784 stars 51 forks source link

freetext_search pretending to be required even though it's not #400

Closed berycz closed 1 year ago

berycz commented 1 year ago

In my custom style I have

foo = Style(
    Field={
        "label__attrs__class__required": lambda field, **_: bool(field.required),
        ...
    },
    ...
)

and then the table__query__form__fields__freetext_search__label gets rendered with class required, even though the rendered html input doesn't have required attribute.

This happens to me on all models so far (I use Table.auto__model)

For example model

class Supplier(models.Model):
    name = models.CharField(
        max_length=255,
        verbose_name=_("Supplier name"),
    )

and table

class SuppliersTable(iommi.Table):
    class Meta:
        auto__model=Supplier
        columns__name__cell__url = lambda row, **_: reverse_lazy("suppliers:edit", kwargs={"pk": row.pk})
        columns__name__filter__include = True
        columns__name__filter__freetext = True
        query__advanced__include = False
        query__form__actions__reset = iommi.ActionFilterReset(attrs__href=reverse_lazy("suppliers:list"))

I also tried using freetext on a different model for only one model field which was blank=True, yet the label still got class required.

For now I "fixed" it with:

foo = Style(
    Query={
        "form__fields__freetext_search__label__attrs__class__required": False,
        ...
    },
    ...
)
jlubcke commented 1 year ago

I started a branch with a first attempt with reproducing this as a test case, but so far no luck :(

berycz commented 1 year ago

aha, found it... I have the customized iommi.Table as specified here: https://docs.iommi.rocks/en/latest/production_use.html

and I have:

class Table(iommi.Table):
    class Meta:
        member_class = Column
        form_class = Form
        query_class = Query
        page_class = Page
        action_class = Action

        # "Search" has cz translate "Hledat" and I need "Hledané slovo"
        query__form__fields__freetext_search__display_name = _("Search term")  # cz: "Hledané slovo"

and the query__form__fields__freetext_search__display_name somehow does it

berycz commented 1 year ago

btw same thing happens when I put it in style instead

foo = Style(
    Field={
        "label__attrs__class__required": lambda field, **_: bool(field.required),
        ...
    },
    Table={
        # "Search" has cz translate "Hledat" and I need "Hledané slovo"
        "query__form__fields__freetext_search__display_name": _("Search term"),  # cz: "Hledané slovo"
    },
    ...
)
jlubcke commented 1 year ago

Interesting! I'll take another stab at trying to reproduce.

berycz commented 1 year ago

Thanks, it works! I even tried (just to be sure)

query__form__fields__freetext_search__input__attrs = {"data-foo": "FOO"}
query__form__fields__freetext_search__label__attrs = {"data-bar": "BAR"}
berycz commented 1 year ago

^ related issue #404 got fixed by this but it's unclear why