Dynamically hide fields in a form

Skip to first unread message

Dax Mitchell

May 18, 2019, 10:33:20 AM5/18/19
to TurboGears


We have a setup like below.  We would really like to hide the password and verify_password fields in the form based on some some conditions which will vary based on parameters set elsewhere (sometimes it's very confusing to show those fields).

The closest thing I could find is this post, which suggests "copying the dynamic field declaration inside the __field_widgets__ property" 

It would be very helpful to see an example, or any other suggestions would be very much appreciated.

class UserAdminConfig(UserControllerConfig):

    class defaultCrudRestController(ResourceManagerCrudRestController):
        __form_options__ = {
            '__require_fields__': ['user_name', 'email_address'],
            '__omit_fields__': ['_password', 'created', '_groups', '_reset_password_key',
                                'reset_password_key', 'reset_password_expires'],
            '__hide_fields__': ['user_id'],
            '__field_order__': ['user_name', 'email_address', 'display_name', 'groups',
                                'password', 'verify_password'],
            '__field_widget_types__': addopts(user_name=TextField,
            '__field_widget_args__': addopts(password=CUSTOM_FORM_FIELD_OPTIONS)

Alessandro Molina

May 21, 2019, 12:38:15 PM5/21/19
to TurboGears
Is https://tw2core.readthedocs.io/en/develop/forms/#dynamic-forms what you are looking for? How to dynamically add/remove fields to a TW2 form?

When you have such more complex customization needs I start to feel it's usually best to switch to hand made TW2 forms where you can inject complex logic in prepare and post_define.

You might be able to do what you want btw, by providing a `__base_widget_type__` to the __form_options__
That will allow you to provide a subclass of tw2.forms.TableForm (or anything else) with a prepare method that hides/removes some fields based on `tg.request.params` or whatever condition you need.  Look at that tw2 docs link, it should explain how to edit the fields of a form during a request and then it should just be a matter of passing your own custom subclass of Form as the __base_widget_type__

You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at https://groups.google.com/group/turbogears.
To view this discussion on the web visit https://groups.google.com/d/msgid/turbogears/0d592ada-62b6-4467-93ab-5ca7f8a619a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dax Mitchell

Jun 29, 2019, 9:15:39 PM6/29/19
to TurboGears
Thanks very much, I appreciate the guidance
To unsubscribe from this group and stop receiving emails from it, send an email to turbo...@googlegroups.com.
Reply all
Reply to author
0 new messages