Data input

Conditional Field

A field that shows while another field of its form has a given value, written as one attribute.

Class nameTypeDescription
fieldplf:fieldComponentA label, a control and its messages
field-labelplf:field-labelPartThe control's label
field-legendplf:field-legendPartThe fieldset's legend
field-groupplf:field-groupModifierA fieldset of fields, without its border

Default

Place the field after the one it depends on, so it appears where the reader goes next.

SMTP server
Plan

Nested

A field inside a hidden one hides with it, and a disabled field has no value, so one condition can follow another.

Webhook

JavaScript

Conditional fields need JavaScript; without it every field shows and submits. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-show-if On any element, usually a field or a fieldset: shows it while its condition is true. Otherwise the script adds hidden and disables every enabled control inside it, so they neither submit nor block the form as required; it enables them again when the element shows, and their values stay

The condition names another field of the same form, or of the root outside a form, by its name:

Condition Shows while the field …
name=value Has this value
name=a,b Has one of these values
name!=value, name!=a,b Has none of them, or no value
name Has a value: checked, chosen or filled in
!name Has no value

A value is what the form would submit, so a disabled field has none; a value cannot contain a comma.

Methods

Method Behavior
PLF.Field.getOrCreateInstance(element) Returns the element’s instance, creating it for valid markup
PLF.Field.getInstance(element) Returns the instance or null
instance.update() Checks the condition now, for example after your code sets a value without sending an event
instance.destroy() Removes the listeners, shows the element and enables the controls the script disabled

With ESM, import Field from @pelatform/ui/components/field.

Events

Event When
plf:field:show After the element shows and its controls are enabled
plf:field:hide After the element hides and its controls are disabled

Each event’s detail:

Property Value
instance The Field instance
reason trigger, or api from update()
trigger The control whose change decided it, or null from update()

Both bubble from the element. Initialization, a form reset and destroy() send no events.

React

@pelatform/react has no Conditional Field component: keep the value a condition reads in state, as each example’s React tab does, and render the field only while the condition holds. A field that is not rendered submits nothing, as a hidden one the script disables does, but it also starts empty when it shows again; keep its value in state too where it should come back, as the script keeps a hidden field’s.


Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons