Skip to content

Commit

Permalink
Allow to remove serieIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 1, 2023
1 parent c4c329d commit 719b76a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Twig/InlineEditBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Book;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
Expand Down Expand Up @@ -39,9 +40,27 @@ public function activateEditing(): void
$this->isEditing = true;
}

/**
* @throws \JsonException
*/
#[LiveAction]
public function save(EntityManagerInterface $entityManager): void
public function save(Request $request, EntityManagerInterface $entityManager): void
{
$all = $request->request->all();
if (!array_key_exists('data', $all)) {
return;
}
if (!is_string($all['data'])) {
return;
}
$data = json_decode($all['data'], true, 512, JSON_THROW_ON_ERROR);
if (!is_array($data)) {
return;
}
if (array_key_exists('updated', $data) && is_array($data['updated']) && array_key_exists('book.serieIndex', $data['updated']) && '' === $data['updated']['book.serieIndex']) {
$this->book->setSerieIndex(null);
}

$entityManager->flush();
$this->dispatchBrowserEvent('manager:flush');
$this->isEditing = false;
Expand Down
9 changes: 9 additions & 0 deletions templates/components/InlineEditBook.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
autofocus
id="book_{{ book.id }}_{{ field }}"
/>
{% elseif field=='serieIndex' %}
<input
type="number"
step="1"
data-model="norender|book.{{ field }}"
class="form-control"
autofocus
id="book_{{ book.id }}_{{ field }}"
/>
{% else %}
<input
type="text"
Expand Down

0 comments on commit 719b76a

Please sign in to comment.