-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional form fields in admin #2172
Comments
See also #381 where this was first touched on (although that was mainly about model logic). I think the very first conversation, around the same time but undocumented, was for some of the Form Builder fields to only appear/disappear based on your choices e.g the choice options would only appear if you added a choice field. |
I would LOVE this added! :) I keep wanting to use OR type logic for field inputs for better CMS admin UX for the front-end solutions I am designing. |
fwiw, https://github.com/octavenz/wagtail-advanced-form-builder does this for the form builder (and end-user input) |
Cool, our use case isn't for building forms on the front-end but for conditional logic for fields in the CMS admin for building more modular templates. |
Would love to see this added too. Three use cases:
|
👍🏼 would also love this |
Plus one and a comment: I've done this (extensively) in Django by using Wagtail-inspired field "control" classes that specify options for toggle (boolean fields) and on select (choice fields). The control instance exports the necessary data-ui-toggle or data-ui-onselect as JSON, and tells the view to load some JavaScript that will parse and act upon this data. Seems to me that FieldPanel could do exactly the same thing. Here are a couple of examples from my UI system: # make EIN visible and required when a company is identified as an employer
controls = {
'is_employer': ui.controls.BooleanControl(
align="center",
css_icon=True,
toggle=ui.forms.Toggle("employer_identification_number", required=True, visible=True)
),
} # make restock_fee, etc. visible when the return_policy is refund, refund-replace, or replace
controls = {
'return_policy': ui.controls.CharControl(
on_select=[
ui.forms.OnSelect(
[
"refund",
"refund-replace",
"replace",
],
[
"restock_fee",
"return_policy_days",
"return_policy_statement",
],
visible=True
)
]
),
} BooleanControl and CharControl (inspired by FieldPanel) know how to add data attributes to the the form field while Toggle and OnSelect tell the view what JavaScript files are needed to create the corresponding UI from the data. The model's wagtailuiplus provided similar functionality using class names and a hook to load the JavaScript. But as of mid 2021, the author has said "there are no plans to update Wagtail UI Plus to the latest version of Wagtail". |
Adding this to the Stimulus project, I'm investigating some custom JS that's used in the admin for conditional field logic. When this code is pulled out to a Stimulus controller, it may be practical to make this behaviour easy to reuse in other admin fields. |
I do this by overriding the
|
+1 |
For those watching this issue. There is a bare minimum start to this in the form of a Stimulus controller. It only supports dynamic disabling but hopefully it will be a building block for more. Any feedback welcome, there is still a way to go even after this is merged before there would be an officially documented Python driven API for this kind of form/widgets though. |
@ZzBombardierzZ please express your interest by adding a thumbs up to the original post. A very short comment like yours is a tad noisy. Thanks for your understanding 😄 |
We should allow certain fields in the page editor to appear/disappear depending on prior selections made.
For instance, let's say you are selecting the author of a blog post. You also have a team members model. You can ask:
Is the author a member of this website? (y/n)
If yes, the following field will be a page chooser panel for team member pages. If no, the following fields will allow you to directly input information about the author of the post.
(has this been requested before? Sorry if so, I couldn't find it)
I think the challenge would be in making an API that doesn't clutter up your real model fields with conditional form code. Perhaps we could have a ConditionalFieldPanel which takes in FieldPanels as arguments. But this should also work with StreamField so maybe we can't do that. Perhaps have a separate mechanism for this entirely.
The text was updated successfully, but these errors were encountered: