Skip to content

Commit

Permalink
Make it possible to add extra form row container attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van den Nieuwenhuisen committed Nov 26, 2020
1 parent 25a8cb5 commit be1b0f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ file that was distributed with this source code.

{% block form_row %}
{% set show_label = show_label ?? true %}
<div class="form-group{% if errors|length > 0 %} has-error{% endif %}" id="sonata-ba-field-container-{{ id }}">
{% set row_id = 'sonata-ba-field-container-'~id %}
{% set row_class = (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim %}
<div{% with {attr: row_attr|merge({id: row_id, class: row_class})} %}{{ block('attributes') }}{% endwith %}>
{% if sonata_admin.field_description.options is defined %}
{% set label = sonata_admin.field_description.options.name|default(label) %}
{% endif %}
Expand Down
17 changes: 17 additions & 0 deletions tests/Form/AdminLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,21 @@ public function testErrors(): void
$expression
);
}

public function testRowAttr(): void
{
$form = $this->factory->createNamed('name', TextType::class, '', [
'row_attr' => [
'class' => 'foo',
'data-value' => 'bar',
],
]);
$view = $form->createView();
$html = $this->renderRow($view);

$this->assertMatchesXpath(
$html,
'//div[@class="foo form-group"][@data-value="bar"][@id="sonata-ba-field-container-name"]'
);
}
}

0 comments on commit be1b0f2

Please sign in to comment.