Back when I was doing most of my code by hand I had a form that consisted of a variable number of div elements that I handled as an array via post upon submission, but I cannot for the life of me figure out how to do anything similar in Symfony and I'm starting to get really frustrated. Here's my original implementation of a template class for an object (with my own simple templating engine):
<div class="img_transfer">
<img class="toggle" src="{{src}}">
<input class="title" type="text" name="title[{{i}}]" placeholder="Title"><br>
Upload to Sta.sh:
<input type="radio" name="stash[{{i}}]" value="0" checked="checked"> No
<input type="radio" name="stash[{{i}}]" value="1"> Yes<br>
Showcase:
<input type="radio" name="showcase[{{i}}]" value="0" checked="checked"> No
<input type="radio" name="showcase[{{i}}]" value="1"> Yes<br>
<input class="save" type="hidden" name="save[{{i}}]" value="0">
<input type="hidden" name="old_path[{{i}}]" value="{{old_path}}">
</div>
The {{i}} was a placeholder for a loop iteration, and this was working fine, but here's how I've been trying to do it in Symfony:
$img_tmp = glob('img_tmp/*.*');
$data = array();
foreach ($img_tmp as $src) {
//$inter = end();
$parts = explode('/', $src);
$file = end($parts);
//$name = explode($inter)[0];
$name = explode('.', $file)[0];
$data[$name] = new Art();
}
$builder = $this->createFormBuilder($data);
foreach ($data as $key => $value) {
//$builder->add($key, (new ArtType())->setSrc($key));
$builder->add($key, ArtType::class, array('img_src' => $key));
}
$form = $builder->getForm();
return $this->render('img_transfer.html.twig', array('form' => $form->createView()));
This is mostly based on another suggestion on SO for how to have a form of objects, but as is it will not let me send the custom option to the form to be built, which I think might be my final barrier to getting this to do what I had easily implemented by hand. I'm wholly welcome to suggestions and would prefer a less hack-y way of doing this, and also would love to know whether I'm just overlooking a simple way of doing this or if this is as horribly un-intuitive as I perceive it to be. I'll edit and restructure this to be a better question when I have time.
Edit: Formatting and the following additional information: This particular part of the application is supposed to take images of arbitrary name and extension dumped in the "web/img_tmp" directory, and move them into the "web/img" directory while renaming them with PHP's uniqid() and entering them into a database; I want something that generates a grouped set of fields for every "/img_tmp/." that allows me to set attributes for each one before persisting them in the DB. All resources I've yet found on how to submit multiple objects at once have been either incomplete or woefully over-complicated.