Allow creatig FridgeProductLine from fridge form
This commit is contained in:
parent
39e028bdca
commit
d4f6b359d5
@ -30,10 +30,14 @@ class FridgeController extends AbstractController
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
foreach ($fridge->getProductLines() as $productLine) {
|
||||
$productLine->setFridge($fridge);
|
||||
$em->persist($productLine);
|
||||
}
|
||||
$em->persist($fridge);
|
||||
$em->flush();
|
||||
|
||||
return $this->redirectToRoute('fridge_list');
|
||||
// return $this->redirectToRoute('fridge_list');
|
||||
}
|
||||
|
||||
return $this->render('fridge/single.html.twig', [
|
||||
|
@ -29,6 +29,11 @@ class Fridge
|
||||
$this->productLines = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
|
16
src/Form/FridgeProductLineType.php
Normal file
16
src/Form/FridgeProductLineType.php
Normal 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,
|
||||
]);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ namespace App\Form;
|
||||
|
||||
use App\Entity\Fridge;
|
||||
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\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
@ -15,7 +16,10 @@ class FridgeType extends AbstractType
|
||||
$builder
|
||||
->add('name')
|
||||
->add('description')
|
||||
->add('productLines')
|
||||
->add('productLines', CollectionType::class, [
|
||||
'entry_type' => FridgeProductLineType::class,
|
||||
'allow_add' => true,
|
||||
])
|
||||
->add('save', SubmitType::class)
|
||||
;
|
||||
}
|
||||
|
17
src/Form/ProductLineType.php
Normal file
17
src/Form/ProductLineType.php
Normal 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')
|
||||
;
|
||||
}
|
||||
}
|
@ -28,6 +28,10 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
|
||||
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') }}#}
|
||||
{% endblock %}
|
||||
</body>
|
||||
|
@ -2,4 +2,35 @@
|
||||
|
||||
{% block body %}
|
||||
{{ 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 %}
|
Loading…
Reference in New Issue
Block a user