8000 [TwigBridge] Add Twig `field_id()` form helper · symfony/symfony@5393f81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5393f81

Browse files
Legendary4226chalasr
authored andcommitted
[TwigBridge] Add Twig field_id() form helper
1 parent b28e597 commit 5393f81

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `is_granted_for_user()` Twig function
8+
* Add `field_id()` Twig form helper function
89

910
7.2
1011
---

src/Symfony/Bridge/Twig/Extension/FormExtension.php

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function getFunctions(): array
6262
new TwigFunction('csrf_token', [FormRenderer::class, 'renderCsrfToken']),
6363
new TwigFunction('form_parent', 'Symfony\Bridge\Twig\Extension\twig_get_form_parent'),
6464
new TwigFunction('field_name', $this->getFieldName(...)),
65+
new TwigFunction('field_id', $this->getFieldId(...)),
6566
new TwigFunction('field_value', $this->getFieldValue(...)),
6667
new TwigFunction('field_label', $this->getFieldLabel(...)),
6768
new TwigFunction('field_help', $this->getFieldHelp(...)),
@@ -93,6 +94,11 @@ public function getFieldName(FormView $view): string
9394
return $view->vars['full_name'];
9495
}
9596

97+
public function getFieldId(FormView $view): string
98+
{
99+
return $view->vars['id'];
100+
}
101+
96102
public function getFieldValue(FormView $view): string|array
97103
{
98104
return $view->vars['value'];

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionFieldHelpersTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ public function testFieldName()
119119
$this->assertTrue($this->view->children['username']->isRendered());
120120
}
121121

122+
public function testFieldId()
123+
{
124+
$this->assertSame('register_username', $this->rawExtension->getFieldId($this->view->children['username']));
125+
$this->assertSame('register_choice_multiple', $this->rawExtension->getFieldId($this->view->children['choice_multiple']));
126+
}
127+
122128
public function testFieldValue()
123129
{
124130
$this->assertSame('tgalopin', $this->rawExtension->getFieldValue($this->view->children['username']));

0 commit comments

Comments
 (0)
0