views:

136

answers:

0

I am trying to dynamically hide fields depending on some data. With dynamically, I don't mean JavaScript, but server-side:

class EditUserForm(TableForm):
    class fields(WidgetsList):
        user_id = HiddenField()
        user_name = TextField(label_text=u"User Name")
        reject_member = SubmitButton(default=u"Reject Membership")
        member_since = CalendarDateTimePicker(label_text=u"member since", disabled=True)
        accept_member = SubmitButton(default=u"Accept Membership")

Just one of the buttons reject_member and accept_member should be displayed, depending whether that user is already a member or not. Renaming that button is not an option because there's other data (member_since) that is displayed if the user is a member. I also don't want to create two different forms because the differences are minor compared to what they have in common (and the number of combinations might grow when I do it with some other field).

What I've tried:

edit_user_form=EditUserForm('edit_user_form', action='do_edit_user',fields = [field for field in EditUserForm.fields() if field.name!='reject_member'])

This creates all fields again, doesn't it? Seems to be a rather bad idea.

<div py:replace="g.edit_user_form(user, **{'.reject_member':{'show':False}})" />

The widget doesn't have a property that simply hides them. Using css-styles "display: None" isn't a nice solution.

<div py:replace="g.edit_user_form(user, **{'.reject_member':{'template':'empty'}})" />

Hides the control, but leaves separator space for it.