You can conditionally display fields based on the values of other fields in your form. For example, if you wanted to display a video embed text field only if the type of article, a radio field, is "video" you'd do something like the following:
1<x-twill::radios 2 name="type" 3 label="Article type" 4 default="long_form" 5 :inline="true" 6 :options="[ 7 [ 8 'value' => 'long_form', 9 'label' => 'Long form article'10 ],11 [12 'value' => 'video',13 'label' => 'Video article'14 ]15 ]"16/>17 18<x-twill::formConnectedFields19 field-name="type"20 field-values="video"21 :render-for-blocks="true" {{-- Depends on the context --}}22>23 <x-twill::input24 name="video_embed"25 label="Video embed"26 />27</x-twill::formConnectedFields>
Here's an example based on a checkbox field where the value is either true or false:
1<x-twill::checkbox 2 name="vertical_article" 3 label="Vertical story" 4/> 5 6<x-twill::formConnectedFields 7 field-name="vertical_article" 8 :field-values="true" 9 :render-for-blocks="true" {{-- Depends on the context --}}10>11 <x-twill::medias12 name="vertical_image"13 label="Vertical Image"14 />15</x-twill::formConnectedFields>
Here's an example based on a checkboxes field where the values are stored in a json field:
1@formField('checkboxes', [ 2 'name' => 'article_target', 3 'label' => 'Target', 4 'min' => 1, 5 'max' => 3, 6 'inline' => true, 7 'options' => [ 8 [ 9 'value' => 'students',10 'label' => 'Students'11 ],12 [13 'value' => 'teachers',14 'label' => 'Teachers'15 ],16 [17 'value' => 'administration',18 'label' => 'Administration'19 ],20 ]21])22 23@formConnectedFields([24 'fieldName' => 'article_target',25 'fieldValues' => 'administration',26 'arrayContains' => false,27 'renderForBlocks' => true/false # (depending on regular form vs block form)28])29 @formField('files', [30 'name' => 'attachment',31 'label' => 'Attachment'32 ])33@endformConnectedFields
Here's an example based on a browser field where the fields are displayed only when the browser field is not empty:
1<x-twill::browser 2 module-name="publication" 3 name="related_publications" 4 label="Related publications" 5 :max="4" 6/> 7 8<x-twill::formConnectedFields 9 field-name="publication"10 :is-browser="true"11 :keep-alive="true"12>13 <x-twill::input14 name="related_publications_header"15 label="Related publications header"16 />17 <x-twill::input18 name="related_publications_copy"19 label="Related publications copy"20 />21</x-twill::formConnectedFields>
Option | Description | Type | Default value |
---|---|---|---|
fieldName | Name of the connected field | string | |
fieldValues | Value or values of the connected field that will reveal the fields in this component's slot | string|array | |
isEqual | Controls how fieldValues are evaluated against the connected field |
boolean | true |
isBrowser | Indicates that the connected field is a browser field |
boolean | false |
arrayContains | Controls how fieldValues are evaluated when connected field is an array |
boolean | true |
matchEmptyBrowser | When set to true, the fields in this component's slot will be revealed when the browser is empty | boolean | false |
keepAlive | When set to true, the state of the hidden fields is preserved | boolean | false |
renderForBlocks | When used inside a block, this needs to be set to true | string | false |