SwitchableFieldDefinition defines a field that displays alternative fields depending on user choice. For example:

  • Switch between a plain text editor and a rich text editor.

  • Authenticated vs. unauthenticated SMTP settings. When the user selects the authenticated option, ask for username and password.

  • In a shop, switch between separate shipping and billing addresses or using the same address for both..

class: info.magnolia.ui.form.field.definition.SwitchableFieldDefinition

Here is a switchable text editor field. It consists of alternative text  and   rich text fields.

Switchable field properties

Simple switchable field definition:

form:
  tabs:
    - name: tabText
      label: Text
      fields:
        - name: message
          class: info.magnolia.ui.form.field.definition.SwitchableFieldDefinition
          label: Message
          options:
            - name: plainText
              label: Plain
              selected: true
              value: plainText
            - name: richText
              label: Rich
              value: richText
          fields:
            - name: plainText
              class: info.magnolia.ui.form.field.definition.TextFieldDefinition
              label: Plain text message
              rows: 5
            - name: richText
              class: info.magnolia.ui.form.field.definition.RichTextFieldDefinition
              label: Rich text message
              height: 200
Node nameValue

 
form


 
tabs


 
tabText


 
fields


 
message


 
options


 
plainText


 
label

 Plain

 
selected

 true

 
value

 plainText

 
richText


 
label

 Rich

 
value

 richText

 
fields


 
plainText


 
class

info.magnolia.ui.form.field.definition.TextFieldDefinition

 
label

Plain text message

 
rows

5

 
richText


 
class

info.magnolia.ui.form.field.definition.RichTextFieldDefinition

 
label

Rich text message

 
height

200

 
class

info.magnolia.ui.form.field.definition.SwitchableFieldDefinition

 
label

Message

You can use common field properties and the following properties in a switchable field definition:

Properties:

<field name>

required

Name of parent switchable field.

options

required

Options (radio buttons) that allow switching between child fields. You can add more than two options.

<option>

required

Options available for selection. Add one node for each option. The number of options should match the number of child fields. .

label

optional

Label displayed next to the radio button.

selected

optional

When set to true the option is selected by default.

(info) Applied only when creating a new item, not for already existing items.

value

optional

Value saved to the repository when selected. Should match the name of the corresponding child field node name.

fields

required

Child fields displayed when the user selects an option.

<child field name>

required

Child field name. Should match the value property of the corresponding option.

class

required

Field definition class of the child field. (warning) Basic upload   field does not work inside a switchable field.

class

required

Definition class of the switchable parent field. Use info.magnolia.ui.form.field.definition.SwitchableFieldDefinition .

layout

optional , default is horizontal

Defines the layout of the radio buttons. Set to vertical for a vertical layout.

selectionType

optional, default is radio

Defines the selection type. Valid values are radio and select.

transformerClass

optional, default is info.magnolia.ui.form.field.transformer.composite.SwitchableTransformer

Custom property transformer definition

Best practice

It can be difficult to configure the correct transformers for advanced fields and especially for fields with complex nesting.

When nesting switchable fields, use delegating field transformers such as the DelegatingCompositeFieldTransformer since normal non-delegating transformers can only handle two nesting levels.

Switchable field and complex nesting

The example configuration below shows the use of the delegating field transformers for a complex nesting scenario where, at the top level, the user has a two-option switch:

The Composite option opens into a composite field with two text fields:

The Multi option opens into a multi field that allows the user to add individual text fields (three are shown):

The field's YAML configuration:

switchComposite: 
  class: info.magnolia.ui.form.field.definition.SwitchableFieldDefinition
  label: Option Switch
  selectionType: radio
  transformerClass: info.magnolia.ui.form.field.transformer.composite.DelegatingCompositeFieldTransformer
  type: String
  options:
    switchInnerComposite:
      label: Composite
      value: switchInnerComposite
    switchInnerMulti:
      label: Multi
      value: switchInnerMulti
  fields:
    switchInnerComposite:
      class: info.magnolia.ui.form.field.definition.CompositeFieldDefinition
      label: Composite Field
      transformerClass: info.magnolia.ui.form.field.transformer.composite.DelegatingCompositeFieldTransformer
      fields:
        text1:
          class: info.magnolia.ui.form.field.definition.TextFieldDefinition
          label: Text Field 1
          type: String
        text2:
          class: info.magnolia.ui.form.field.definition.TextFieldDefinition
          label: Text Field 2
          type: String
    switchInnerMulti:
      class: info.magnolia.ui.form.field.definition.MultiValueFieldDefinition
      label: Multi Field
      required: false
      transformerClass: info.magnolia.ui.form.field.transformer.multi.DelegatingMultiValueFieldTransformer
      field:
        class: info.magnolia.ui.form.field.definition.MultiValueFieldDefinition
        name: newSubMultiple
        required: false
        transformerClass: info.magnolia.ui.form.field.transformer.multi.DelegatingMultiValueFieldTransformer
        field:
          class: info.magnolia.ui.form.field.definition.TextFieldDefinition
          name: nameProperty
          type: String
#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels