Allow creatig FridgeProductLine from fridge form

This commit is contained in:
Tim 2022-01-02 18:56:28 +01:00
parent 39e028bdca
commit d4f6b359d5
7 changed files with 83 additions and 2 deletions

View File

@ -30,10 +30,14 @@ class FridgeController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
foreach ($fridge->getProductLines() as $productLine) {
$productLine->setFridge($fridge);
$em->persist($productLine);
}
$em->persist($fridge); $em->persist($fridge);
$em->flush(); $em->flush();
return $this->redirectToRoute('fridge_list'); // return $this->redirectToRoute('fridge_list');
} }
return $this->render('fridge/single.html.twig', [ return $this->render('fridge/single.html.twig', [

View File

@ -29,6 +29,11 @@ class Fridge
$this->productLines = new ArrayCollection(); $this->productLines = new ArrayCollection();
} }
public function __toString(): string
{
return $this->name;
}
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;

View File

@ -0,0 +1,16 @@
<?php
namespace App\Form;
use App\Entity\FridgeProductLine;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FridgeProductLineType extends ProductLineType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => FridgeProductLine::class,
]);
}
}

View File

@ -4,6 +4,7 @@ namespace App\Form;
use App\Entity\Fridge; use App\Entity\Fridge;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
@ -15,7 +16,10 @@ class FridgeType extends AbstractType
$builder $builder
->add('name') ->add('name')
->add('description') ->add('description')
->add('productLines') ->add('productLines', CollectionType::class, [
'entry_type' => FridgeProductLineType::class,
'allow_add' => true,
])
->add('save', SubmitType::class) ->add('save', SubmitType::class)
; ;
} }

View File

@ -0,0 +1,17 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
abstract class ProductLineType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('product')
->add('count')
;
}
}

View File

@ -28,6 +28,10 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script
src="https://code.jquery.com/jquery-3.6.0.slim.min.js"
integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI="
crossorigin="anonymous"></script>
{# {{ encore_entry_script_tags('app') }}#} {# {{ encore_entry_script_tags('app') }}#}
{% endblock %} {% endblock %}
</body> </body>

View File

@ -2,4 +2,35 @@
{% block body %} {% block body %}
{{ form(form) }} {{ form(form) }}
<button type="button" class="btn btn-primary add_item_link" data-collection-holder-id="fridge_productLines">Add product</button>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script>
const addFormToCollection = (e) => {
const collectionHolder = document.querySelector('#' + e.currentTarget.dataset.collectionHolderId);
console.log(collectionHolder);
const item = document.createElement('div');
item.innerHTML = collectionHolder
.dataset
.prototype
.replace(
/__name__label__/g,
collectionHolder.children.length
)
.replace(
/__name__/g,
collectionHolder.children.length
);
collectionHolder.appendChild(item);
};
document
.querySelectorAll('.add_item_link')
.forEach(btn => btn.addEventListener("click", addFormToCollection));
</script>
{% endblock %} {% endblock %}