From d78e0fa4fc136c4257303bd7c26c510e781e81c6 Mon Sep 17 00:00:00 2001 From: Rafikooo Date: Fri, 9 Oct 2026 16:28:47 +0200 Subject: [PATCH] fix(bootstrap-admin-ui): pick the form method from the page instead of the resource id --- src/AdminUi/templates/crud/create.html.twig | 2 +- src/AdminUi/templates/crud/update.html.twig | 2 +- .../shared/crud/common/content/form.html.twig | 4 ++ .../src/Resource/BookResource.php | 7 ++- .../tests/Functional/FormMethodTest.php | 43 +++++++++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/BootstrapAdminUi/tests/Functional/FormMethodTest.php diff --git a/src/AdminUi/templates/crud/create.html.twig b/src/AdminUi/templates/crud/create.html.twig index 39fc882a..fd484548 100644 --- a/src/AdminUi/templates/crud/create.html.twig +++ b/src/AdminUi/templates/crud/create.html.twig @@ -14,7 +14,7 @@ {% endblock %} {% block body %} - {% hook 'create' with { _prefixes: prefixes, resource, metadata, configuration, form } %} + {% hook 'create' with { _prefixes: prefixes, resource, metadata, configuration, form, form_method: 'POST' } %} {% endblock %} {% block stylesheets %} diff --git a/src/AdminUi/templates/crud/update.html.twig b/src/AdminUi/templates/crud/update.html.twig index 3db8e694..faa6fe94 100644 --- a/src/AdminUi/templates/crud/update.html.twig +++ b/src/AdminUi/templates/crud/update.html.twig @@ -14,7 +14,7 @@ {% endblock %} {% block body %} - {% hook 'update' with { _prefixes: prefixes, resource, metadata, configuration, form } %} + {% hook 'update' with { _prefixes: prefixes, resource, metadata, configuration, form, form_method: 'PUT' } %} {% endblock %} {% block stylesheets %} diff --git a/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig b/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig index dacf3d35..1270b4ca 100644 --- a/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig +++ b/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig @@ -14,6 +14,10 @@
{% if hookable_metadata.configuration.method is defined %} + {% elseif hookable_metadata.context.form_method is defined %} + {% if hookable_metadata.context.form_method != 'POST' %} + + {% endif %} {% elseif form.vars.data.id|default(null) is not null %} {% endif %} diff --git a/src/BootstrapAdminUi/tests/Functional/.application/src/Resource/BookResource.php b/src/BootstrapAdminUi/tests/Functional/.application/src/Resource/BookResource.php index 22d91e00..d5bdf033 100644 --- a/src/BootstrapAdminUi/tests/Functional/.application/src/Resource/BookResource.php +++ b/src/BootstrapAdminUi/tests/Functional/.application/src/Resource/BookResource.php @@ -28,7 +28,7 @@ driver: false, operations: [ new Index(grid: BookGrid::class), - new Create(), + new Create(factory: [self::class, 'withGeneratedId']), new Update(provider: BookItemProvider::class), ], )] @@ -40,6 +40,11 @@ public function __construct( ) { } + public static function withGeneratedId(): self + { + return new self(bin2hex(random_bytes(8))); + } + public function getId(): ?string { return $this->id; diff --git a/src/BootstrapAdminUi/tests/Functional/FormMethodTest.php b/src/BootstrapAdminUi/tests/Functional/FormMethodTest.php new file mode 100644 index 00000000..f8bff88f --- /dev/null +++ b/src/BootstrapAdminUi/tests/Functional/FormMethodTest.php @@ -0,0 +1,43 @@ +client = self::createClient(); + } + + public function testCreateFormIsPostedEvenWhenTheNewResourceAlreadyHasAnId(): void + { + $this->client->request('GET', '/books/new'); + + $this->assertResponseIsSuccessful(); + $this->assertSelectorNotExists('form input[name="_method"]'); + } + + public function testUpdateFormIsPut(): void + { + $this->client->request('GET', '/books/the-shining/edit'); + + $this->assertResponseIsSuccessful(); + $this->assertSelectorExists('form input[name="_method"][value="PUT"]'); + } +}