Symfony 6 - how to use CollectionType Field - forms

Config : Symfony 6.1, PHP 8.1.4, Xampp
I have 3 entities: Carousel, Picture and CarouselPicture.
CarouselPicture is the entity created to make a join table in the ManyToMany relationship with attribute that exists between Carousel and Picture. For each entity there is a FormType.
In my CarouselType (the form for Carousel), I would like to dynamically add fields from CarouselPictureType.
After reading the docs, I understand that you can add a CollectionType... except I can't get it to work.
I probably missed something somewhere but I can't find it.
What I did (cf CarouselType) does not return an error but does not create the fields in question as can be seen below : https://i.stack.imgur.com/g0PUT.png
Here are the files involved in the problem
CarouselType : https://sharemycode.io/c/6ff7150
CarouselPictureType : https://sharemycode.io/c/f6edfab
Entity Carousel : https://sharemycode.io/c/a10ffac
Entity CarouselPicture https://sharemycode.io/c/97021c7
The Twig view of the Carousel form (in the comment an illustration of the output I would like to obtain) :
https://sharemycode.io/c/85235bf

Related

Use an EntityType Form for Edit and New in EasyAdmin 3 and symfony 5

I'm learning symfony 5 with EasyAdmin 3.
I created a FormType for my entity, with label, attr, constraints (min, max). But to create or update my entity I use easyAdmin 3 and the constraints are not respected.
I would like to create a Form (from my entity) and use it in crud/edit and crud/new template.
I override the template in my entityCrudController but I don't know how to integrate the entityForm inside.
thank you for your reply and sorry for my English :D
you could use event. Something like
BeforeEntityPersistedEvent::class
With a checking
if ($entity instanceof $yourentity)
to make it trigger for the right entity and control if the data match the restriction, and
throw $this->createNotFoundException
if it doesnt.
This way it will check each time before change the database.

Data in array collection shows empty when retrieving info

I'm creating a web app with symfony. I'm currently building the forms and as I've never used the ManyToMany relation I'm having some problem with retrieving the information.
The form I'm working with is this one:
The point is that when I'm retrieving the information of the array in the twig template, the data property inside the array shows empty, when there is Alumne's entities created. Let me show you.
Twig template (create page):
As you can see, what I pretend is to retrieve the info of every Alumne entity inside the array.
What I get doing this is:
The alumnes field is completly empty. But if I change the form Builder to this:
(I have also changed a little bit the twig template to make it more readable)
It works!
And as you can see there is an Alumne created.
The point is, as you can see, the select and option tag it creates is kinda ugly, I want to custom so it can fit the rest of the forms. So that's why I need a CollectionType in the builder and I don't know what I'm doing wrong. Also if I check the dump(form.alumnes.vars.data) it shows empty.
The issue here is, that you use the CollectionType instead of the EntityType. To load data from the Database into a form, you should use the EntityType https://symfony.com/doc/current/reference/forms/types/entity.html
If you leave the type out, Symfony will try to guess what form element to use and will (correctly) guess the EntityType

TYPO3 Extbase & Fluid Grouped output with attribute from m:n relation

I'm using some domain with m:n relations in an Extbase TYPO3-Extension and want to get an output grouped by an attribute from a m:n-relation child, ideally respecting including entries having several relations.
Let's say I have Books and Categories. Books have some "title" and a "memberOfCategories" attribute, Categories have some "name" and a "areBooks" attribute. The general m:n thing (using some mn-table and the necessary fields in TCA and so on) works fine.
Using fluid, output of all categories of a book works fine, as well as all books having a certain category and so on.
But I need (in the book view / action context!) some output like that (including "duplicate" output of books belonging to more than one category):
Category A
Book A
Book B
Category B
Book A
Book C
(No Category)
Book D
Book E
I tried using the f:groupedFor ViewHelper, but that does not work as I want. Grouping by the child attribute (category.name) does not work as desired:
<f:groupedFor each="{books}" as="booksOfThisCat" groupBy="memberOfCategories.name" groupKey="memberOfCategories.name">
<h3>{memberOfCategories.name} (should be category name)</h3>
<f:for each="{booksOfThisCat}" as="book">
<li>{book.title}</li>
</f:for>
</f:groupedFor>
My first try was grouping just by memberOfCategories - but that leads to a different and unuseful result as well, because memberOfCategories only references to the "number" / set of relations, so the grouping is rubbish.
Sure that would all be very easy if I worked within an action of Categories, there it would just be a simple for each category { for each areBooks } thing, but I need that output in an action / view regarding books.
I'm pretty much sure I'm just too dumb or blind - and may have confused some readers with the slightly pidgeon English ;-), anyhow, it would be great if anybody had some hint for that. Found some similar question regarding grouping by dates, solved with a custom view helper, but I thought there might / should be an easier solution.
Oh oh - now I just got behind it on myself, it's just that simple:
Add / inject the category-Repository to my books-controller and assign its desired output (e.g. findall) to my action / view and everything is fine es it already was under the category-controller.

Does my POST edit action overwrite all fields in EF?

Say I have a database record with 7 fields and I just want to edit the contents of field 1. So I hit my GET EDIT action, it renders the strongly typed view with my viewmodel and I go ahead and update field 1. However, my POST action contains 'mapping' for all fields as below. So will entity framework 'overwrite' all 6 other fields in the underlying database with the unchanged value or will it only just change field 1?
Just looking for further clarification after giving the previous related answer (c# edit method overwrite all or only save changes fields?) some more thought given that I am explicitly specifying each field in the edit method.
EDIT POST method extract -
db.entities.find(x)
entity.field1= viewmodel.field1;
entity.field2= viewmodel.field2;
entity.field3= viewmodel.field3;
entity.field4= viewmodel.field4;
entity.field5= viewmodel.field5;
entity.field6= viewmodel.field6;
entity.field7= viewmodel.field7;
db.savechanges()
FYI - this is my first proper MVC app.
Many thanks

Zend Framework Dynamically added fields of a form and populate

I have been attempting to create a form where a user can simply press a button and the form will add a new field for the user to use. I have 2 of these dynamically added field types.
Firstly a field where a user can upload files, by pressing the add button another field is pasted underneath the current field and is ready for use.
I have followed an old guide on how to get this done with a bit of ajax and jQuery.
This guide to be exact: http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/
As you can see it's from 2009 and a bit outdated yet it still works under the current Zend Framework version 1.11.11
The problem however arises now that i want an edit / update version of the form. I need to populate it's fields but first of all i need to create enough fields for the data to be stored in. So when there's 3 files that have been uploaded it should create 2 additional fields and place the 3 file names in these fields ready to be edited and updated. Simply using $form->populate($stuff) is not going to work
I just have no idea how to accomplish this and the tutorial on dynamically added fields only goes as far as the addAction and not how to create the editAction under these conditions.
Is there any tutorial out there on how to create and manage forms such as these? I'm sure i am not the only one who's had the idea to builds these kind of forms?
I can add my code if there's a request for it but it's the same as the example from the guide, just a different set of elements in the form.
Adding a small example of it's use.
A user adds an item with 3 files, these files are uploaded along with a filename so in the database it appears like this : File_Id : '1' , File_Name : 'SomeFile' , File_location : 'somewhere/on/my/pc/SomeFile.txt'.
Now the user realizes he forgot a file or wants to delete a file from that list, he goes to the edit page and here i want the form to display the previously added filenames. So if there's 3 files it shows 3 and when there's 2 it shows 2 etc. How do i build a form to dynamically add fields based on the number of uploaded files and then populate them?
Any advice on how to handle this is well appreciated :)
You can make use of the semi-magic setXxx() methods of the form.
Inside the form:
public function setFiles($files) {
foreach ($files as $file) {
$this->addElement(/* add a file element */);
//do other stuff, like decorators to show the file name, etc.
}
}
In your controller:
$files = $model->getFiles();
$form = new Form_EditFiles(array('files' => $files));
By passing an array with key files you will make the form try to call the method named setFiles(), which you have conveniently provided above.
This should push you in the right direction, or so I hope at least.
If I understand you correctly you want to populate file upload fields, which is not possible because of security reasons.
Edit:
You can add Elements inside of the Controller via $form->addElement() (basicly just like the $this->addElement() statements in the Tutorial)