I am trying to create a generic form to import the CSV the $ data represents a complete line of csv data.
I'm trying to generate a response to register the data using form
public function save(\Symfony\Component\Form\Form $form, $data ){
// Campos do formulário
$fields = $form->all();
$i = 0;
$r = new Request();
$r->setMethod('POST');
foreach ($fields as $f) {
$r->request->set($f, $data[$i++]);
}
$form->submit($r);
$form->handleRequest($r);
if (!$form->isValid()) {
throw new Exception($form->getErrors());
}
$entity = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return true;
}
The form is always invalid, this is a generic function. The main purpose of it is to receive a form to save the data, popular the entity related to it and after, write to the persistence layer, how can I do this by getting Form as parameter?