diff --git a/resources/views/extend/forms/fields.antlers.html b/resources/views/extend/forms/fields.antlers.html index 140c41c90a..519bc79910 100644 --- a/resources/views/extend/forms/fields.antlers.html +++ b/resources/views/extend/forms/fields.antlers.html @@ -6,8 +6,11 @@ * {{ /if }} -
{{ field }}
- {{ if instructions }} + {{ if instructions && instructions_position == "above" }} +

{{ instructions }}

+ {{ /if }} +
{{ field }}
+ {{ if instructions && instructions_position == "below" }}

{{ instructions }}

{{ /if }} {{ if error }} diff --git a/src/Forms/FieldsVariable.php b/src/Forms/FieldsVariable.php new file mode 100644 index 0000000000..542333370d --- /dev/null +++ b/src/Forms/FieldsVariable.php @@ -0,0 +1,21 @@ + $fields])->render(), + $fields + ); + } + + public function toArray() + { + return $this->extra; + } +} diff --git a/src/Forms/Tags.php b/src/Forms/Tags.php index f44018e7c6..8a7d816043 100644 --- a/src/Forms/Tags.php +++ b/src/Forms/Tags.php @@ -74,7 +74,7 @@ public function create() $data['sections'] = $this->getSections($this->sessionHandle(), $jsDriver); - $data['fields'] = collect($data['sections'])->flatMap->fields->all(); + $data['fields'] = new FieldsVariable(collect($data['sections'])->flatMap->fields->all()); $data['honeypot'] = $form->honeypot(); diff --git a/tests/Tags/Form/FormCreateTest.php b/tests/Tags/Form/FormCreateTest.php index 3be2cc95ad..cbbd668037 100644 --- a/tests/Tags/Form/FormCreateTest.php +++ b/tests/Tags/Form/FormCreateTest.php @@ -5,10 +5,13 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Storage; +use Illuminate\Testing\Assert as PHPUnit; +use Illuminate\Testing\Constraints\SeeInOrder; use Illuminate\Validation\ValidationException; use PHPUnit\Framework\Attributes\Test; use Statamic\Facades\AssetContainer; use Statamic\Facades\Form; +use Statamic\Forms\FieldsVariable; use Statamic\Statamic; class FormCreateTest extends FormTestCase @@ -74,6 +77,26 @@ public function it_dynamically_renders_fields_array() $this->assertEquals(['Full Name', 'Email Address', 'Message'], $fieldOrder[1]); } + #[Test] + public function it_dynamically_renders_fields_view_using_single_tag() + { + $output = $this->normalizeHtml($this->tag(<<<'EOT' +{{ form:contact }} + {{ fields }} +{{ /form:contact }} +EOT + )); + + PHPUnit::assertThat([ + '', + '', + '', + '', + '', + '', + ], new SeeInOrder($output)); + } + #[Test] public function it_dynamically_renders_with_form_handle() { @@ -504,20 +527,20 @@ public function it_dynamically_renders_sections_array() $output = $this->normalizeHtml($this->tag(<<<'EOT' {{ form:survey }} {{ sections }} -
{{ if display}}{{ display }} - {{ /if }}{{ if instructions }}{{ instructions }} - {{ /if }}{{ fields | pluck('handle') | join(',') }}
+
{{ if display}}{{ display }} - {{ /if }}{{ if instructions }}{{ instructions }} - {{ /if }}{{ fields }}[{{ handle }}]{{ /fields }}
{{ /sections }} -
{{ fields | pluck('handle') | join(',') }}
+
{{ fields }}[{{ handle }}]{{ /fields }}
{{ /form:survey }} EOT )); - $this->assertStringContainsString('
One - One Instructions - alpha,bravo
', $output); - $this->assertStringContainsString('
Two - Two Instructions - charlie,delta
', $output); - $this->assertStringContainsString('
echo,fox
', $output); + $this->assertStringContainsString('
One - One Instructions - [alpha][bravo]
', $output); + $this->assertStringContainsString('
Two - Two Instructions - [charlie][delta]
', $output); + $this->assertStringContainsString('
[echo][fox]
', $output); // Even though the fields are all nested within sections, // we should still be able to get them via `{{ fields }}` array at top level... - $this->assertStringContainsString('
alpha,bravo,charlie,delta,echo,fox
', $output); + $this->assertStringContainsString('
[alpha][bravo][charlie][delta][echo][fox]
', $output); } #[Test] @@ -835,7 +858,7 @@ public function it_fetches_form_data() $this->assertArrayHasKey('_token', $form['params']); $this->assertIsArray($form['errors']); - $this->assertIsArray($form['fields']); + $this->assertInstanceOf(FieldsVariable::class, $form['fields']); $this->assertEquals($form['honeypot'], 'winnie'); $this->assertEquals($form['js_driver'], 'alpine');