Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/AdminUi/templates/crud/create.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
2 changes: 1 addition & 1 deletion src/AdminUi/templates/crud/update.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<div class="card-body">
{% if hookable_metadata.configuration.method is defined %}
<input type="hidden" name="_method" value="{{ hookable_metadata.configuration.method }}"/>
{% elseif hookable_metadata.context.form_method is defined %}
{% if hookable_metadata.context.form_method != 'POST' %}
<input type="hidden" name="_method" value="{{ hookable_metadata.context.form_method }}"/>
{% endif %}
{% elseif form.vars.data.id|default(null) is not null %}
<input type="hidden" name="_method" value="PUT"/>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
)]
Expand All @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions src/BootstrapAdminUi/tests/Functional/FormMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Tests\Sylius\BootstrapAdminUi\Functional;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class FormMethodTest extends WebTestCase
{
private KernelBrowser $client;

protected function setUp(): void
{
$this->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"]');
}
}
Loading