question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional form fields in admin

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:8
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
joegarlickcommented, Jul 1, 2021

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.

0reactions
develmaycarecommented, Oct 10, 2022

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 clean() method is used to enforce client side logic.

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”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional Form Fields | Form API - Drupal
Conditional Form Fields ... Drupal's Form API #states property allows to easily show or hide, enable or disable, require or collapse form fields...
Read more >
Create conditional form fields - Finalsite Support
Conditional fields are form fields that only show up for a user when certain criteria are met. This helps to streamline your form...
Read more >
How Conditional Fields Can Clean Up Your Admin - TypeRocket
Conditional fields help you create a refined user experience by allowing you to hide select fields when they are not needed.
Read more >
Creating Conditionally Required Fields in Django Forms
This method works if you have only one or a few conditionally required fields, but as soon as you have more, this code...
Read more >
Conditional Fields and Ticket Form Layout - Deskpro Support
Conditional Fields and Ticket Form Layout ... When you add a field to a Ticket form you can amend how it will behave...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found