emmett-framework / emmett

The web framework for inventors
Other
1.03k stars 70 forks source link

RadioWidget #337

Closed Kkeller83 closed 3 years ago

Kkeller83 commented 3 years ago

Would it be hard to add a RadioWidget to forms please? :) I would like to write a "polls" type of app.

gi0baro commented 3 years ago

I can add a radio widget to standard FormStyle. In the meantime you can define a custom widget, something like:

from emmett.forms import FormStyle
from emmett.html import cat, tag

def radio_widget(field, value):
    options, _ = FormStyle._field_options(field)
    return cat(*[
        cat(
            tag.input(
                _id=f"{field.name}_{k}",
                _name=field.name,
                _value=k,
                _type="radio",
                _checked=("checked" if str(k) == str(value) else None)
            ),
            tag.label(n, _for=f"{field.name}_{k}")
        ) for k, n in options
    ])

and use it in your models or fields, see https://emmett.sh/docs/2.2.x/orm/models#widgets

Note: untested code

Kkeller83 commented 3 years ago

Thank you!!!