Skip to main content

conditional_field

Allows for arbitrary showing and hiding of fields in the admin based on certain user input. Idea based on Wagtail UI+'s conditional visibility fields.

Installations

  1. Add conditional field to your INSTALLED_APPS

    INSTALLED_APPS = [
        ...
        'conditional_field'
        ...
    ]
    
  2. Run python3 ./manage.py collectstatic

Example usage of the gcf conditional fields.

from wagtail import blocks


class Link(blocks.StructBlock):
    text = CharBlockWithAttrs(
        required=False,
        label=_("Text"),
    )

    page = blocks.PageChooserBlock(
        required=False,
        label=_("Page"),
        classname=(
            "gcf "
            "gcf-handler--choice "
            "gcf-action--show--page "
        ),
    )

    document = DocumentChooserBlock(
        required=False,
        label=_("Document"),
        classname=(
            "gcf "
            "gcf-handler--choice "
            "gcf-action--show--document"
        ),
    )

    external_link = blocks.URLBlock(
        required=False,
        label=_("External Link"),
        classname=(
            "gcf "
            "gcf-handler--choice "
            "gcf-action--show--external_link"
        ),
    )

    email = blocks.EmailBlock(
        required=False,
        label=_("Email"),
        classname=(
            "gcf "
            "gcf-handler--choice "
            "gcf-action--show--email"
        ),
    )

    phone = blocks.CharBlock(
        required=False,
        label=_("Tel"),
        placeholder=_("123-456-7890"),
        validators=[
            RegexValidator(
                regex=r"^\+?[0-9\-\s\(\)]+$",
                message=_("Enter a valid phone number."),
            )
        ],
        min_length=7,
        max_length=14,
        classname=(
            # Example of multiple allowed
            # choices for a single block
            # We introduce fshow and fhide,
            # these do not automatically do the opposite for false values.
            # You would not want to use this in this context; it is just an example.
            "gcf "
            "gcf-handler--choice "
            "gcf-action-any--fhide "
            "gcf-action--fshow--email "
            "gcf-action--fshow--tel"
        ),
    )

    choice = blocks.ChoiceBlock(
        required=True,
        choices=[
             ("page", "Page")
             ("document", "Document")
             ("external_link", "External")
             ("email", "Email Link")
             ("tel", "Telephone Link")
        ],
        default="page",
        label=_("Link Type"),
        classname=(
            "gcf "
            "gcf-handler--choice"
        ),
        widget=forms.RadioSelect,
    )



hide_animation_fields_classname = (
    'gcf '
    'gcf-handler--animation '
    'gcf-action-empty--hide '
    'gcf-action-any--show'
)

class AnimatorBlock(blocks.StructBlock):
    animation = blocks.ChoiceBlock(
        choices=animations_choices,
        required=False,
        label=_("Animation"),
        translatable=False,
        classname=(
            'gcf '
            'gcf-handler--animation'
        ),
    )

    duration = blocks.IntegerBlock(
        default=1000,
        required=True,
        translatable=False,
        label=_("Duration"),
        classname=hide_animation_fields_classname,
    )

    delay = blocks.IntegerBlock(
        default=0,
        required=True,
        translatable=False,
        label=_("Delay"),
        classname=hide_animation_fields_classname,
    )

Metadata

Release files for conditional-field 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for conditional-field 1.1.0
File Size Uploaded
conditional_field-1.1.0.tar.gz 12.7 kB Details

Release files / conditional_field-1.1.0.tar.gz

Download URL conditional_field-1.1.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
74133883333f1dc4fd63a740dca248b9d16209cdce6ee1647567793ace1792b9
BLAKE2b-256 checksum
How to use checksums
9c0e316de8e5f05bdfee9226a7af57cfba91b1dba5da8949e08fe1512fd37d1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.6

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page