Search code examples
plonedexterity

Hiding dexterity field according user property


While adding dexterity object, I have two fields in which we like to display only one field according to user property which is saved in members data.

Schema definition is somewhat like:

class IMyApplication(form.schema):
      fieldA = schema.textline(...)
      fieldB = schema.textline(...)

I would like to show one fields out of these two field to user according to their user property.

Many thanks in advance for guide/link/advise .


Solution

  • First please consider the example for a custom add/edit form in the DX-docu

    Then you are able to hide a field in the updateWidgets method of the form.

    Example Code (not tested):

    from plone.dexterity.browser.add import DefaultAddForm
    from z3c.form import interfaces
    
    
    class AddForm(DefaultAddForm):
    
        def updateWidgets(self):
            super(AddForm, self).updateWidgets()
    
            if my_condition:
                self.widgets['myfield'].mode = interfaces.HIDDEN_MODE
    

    Since you have to do the same in the add and edit form, you could create a mixin class with your customization.