Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the alignment of form fields in some cases #6003

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Factory/FormLayoutFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ private function linearizeLayoutConfiguration(FieldCollection $fields): void
{
$formUsesColumns = false;
$formUsesTabs = false;
$formUsesFieldsets = false;
foreach ($fields as $fieldDto) {
if ($fieldDto->isFormColumn()) {
$formUsesColumns = true;
continue;
}

if ($fieldDto->isFormTab()) {
$formUsesTabs = true;
}
match (true) {
$fieldDto->isFormColumn() => $formUsesColumns = true,
$fieldDto->isFormTab() => $formUsesTabs = true,
$fieldDto->isFormFieldset() => $formUsesFieldsets = true,
default => null,
};
}

$aFormColumnIsOpen = false;
Expand Down Expand Up @@ -269,6 +268,14 @@ private function linearizeLayoutConfiguration(FieldCollection $fields): void
$fields->prepend($this->createTabPaneGroupOpenField());
$fields->prepend($this->createTabListField($tabs));
}

// if the form doesn't use any layout fields (tabs, columns, fieldsets, etc.),
// wrap all fields inside a fieldset to simplify the rendering of the form later
// (by default, this fieldset is invisible and doesn't change the form layout, so it's fine)
if (!$formUsesTabs && !$formUsesColumns && !$formUsesFieldsets) {
$fields->prepend($this->createFieldsetOpenField());
$fields->add($this->createFieldsetCloseField());
}
}

private function createColumnGroupOpenField(bool $formUsesTabs): FieldDto
Expand Down
2 changes: 0 additions & 2 deletions src/Resources/views/crud/detail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@

{% block main %}
{% block detail_fields %}
<div class="row">
{% for field in entity.fields %}
{% if field.isFormLayoutField %}
{{ _self.render_layout_field(field) }}
{% else %}
{{ _self.render_field_contents(entity, field) }}
{% endif %}
{% endfor %}
</div>
{% endblock detail_fields %}

{% block delete_form %}
Expand Down
10 changes: 4 additions & 6 deletions src/Resources/views/crud/form_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

{{ parent() }}

<div class="row"> {# this is closed in form_end #}
<input type="hidden" name="referrer" value="{{ ea.request.query.get('referrer') }}">
<input type="hidden" name="referrer" value="{{ ea.request.query.get('referrer') }}">
{% endblock form_start %}

{% block form_end %}
{% if not render_rest is defined or render_rest %}
{{ form_rest(form) }}
{% endif %}
</div> {# this closes the '<div class="row">' of form_start #}
{% if not render_rest is defined or render_rest %}
{{ form_rest(form) }}
{% endif %}
</form>
{% endblock %}

Expand Down
10 changes: 6 additions & 4 deletions tests/Factory/FormLayoutFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ public function testFixFormColumnsErrors(array $originalFields, string $expected

public function provideFormLayouts()
{
yield 'Only fields' => [
yield 'Only fields (a fieldset is added automatically to wrap all fields)' => [
['field', 'field', 'field'],
<<<LAYOUT
field
field
field
fieldset_open
field
field
field
fieldset_close
LAYOUT,
];

Expand Down