you can remove a field from a form based on conditions
This way as I understand it different conditions can change what is shown for edit - and without removing already saved values of the field that was there
I gave this a quick shot with a custom behavior here using p.a.z3cform==4.3.0 (no generic attribute feature)
General disabled field:
from plone.autoform.interfaces import IFormFieldProvider
from plone.autoform import directives
from plone.supermodel import model
from z3c.form.browser.text import TextFieldWidget
from zope.interface import Interface
from zope.interface import provider
class ITestDisabledFieldMarker(Interface):
pass
@provider(IFormFieldProvider)
class ITestDisabledField(model.Schema):
"""
"""
project = schema.TextLine(
title=_(u'Project'),
description=_(u'Give in a project name'),
required=False,
)
directives.widget(
"project",
TextFieldWidget,
disabled="disabled",
)
This now shows a disabled input field in my form when the behavior is enabled somewhere:
Need to check (later) why 'group' matters, since I also have this in another fieldset, where this did not work:
def update(self):
super(MyContentTypeEditForm, self).update()
for group in self.groups:
if group.__name__ == 'some_fieldset':
group.widgets['my_field'].disabled='disabled'
Probably, it needs to be done somewhere else (than def update)
So far, it does not seem like it matters where this is 'updated', it 'just does not work for fieldsets'.
Note setting .mode (instead of .disabled) works, so maybe it is a bug somewhere (?)
I just had this once more in a project of mine and I have more details on this. The following code snipped (basically copied from the docs here Forms – Classic UI — Plone Documentation v6.0 and extended it) has a default and a personal_info fieldset.
from plone import schema
from plone.app.z3cform.widgets.datetime import DateFieldWidget
from plone.autoform import directives
from plone.autoform.form import AutoExtensibleForm
from plone.supermodel.directives import fieldset
from z3c.form import button
from z3c.form import form
from z3c.form.browser.text import TextFieldWidget
from zope.interface import Interface
class IMyForm(Interface):
""" Schema Interface for IMyForm
Define your form fields here.
"""
# field in default fieldset
test_field = schema.TextLine(
title="Default Field"
)
fieldset(
"personal_info",
label="Personal Information",
fields=["name", "surname", "birthday"],
)
name = schema.TextLine(
title="Your name",
)
surname = schema.TextLine(
title="Your surname",
)
directives.widget(
"surname",
TextFieldWidget,
disabled="disabled",
)
birthday = schema.Date(
title="Birthday",
)
directives.widget(
"birthday",
DateFieldWidget,
disabled="disabled",
)
class MyForm(AutoExtensibleForm, form.EditForm):
schema = IMyForm
ignoreContext = True
This generates a disabled TextFieldWidget on the second tab, but the DateFieldWidget is not disabled.
This is because the "pattern widget" implementation in plone.app.z3cform misses this property obviously. So this doesn't have anything to do with auto extensible group forms but with widget template implementation in p.a.z3cform.
I've tested this with latest plone.app.z3cform==4.4.0 and z3c.form==5.1 and with a minimal change to the import (to get Bootstrap TextFieldWidget):
from plone.app.z3cform.widgets.text import TextFieldWidget
Just for the record: it was not possible to change wiget template either – that was the workaround I (also) tried ( .template = Z3ViewPageTemplateFile("some.pt") )
There might be a caveat with disabled fields because this doesn't get submitted in the request AFAIK. So if you have a prefilled disabled field in the edit form, the field might be emptied if you save that form. But I didn't test that.