Skip to content

Instantly share code, notes, and snippets.

@alpha1125
Created January 3, 2021 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpha1125/b089b4cd84860aded120ee572fca8b50 to your computer and use it in GitHub Desktop.
Save alpha1125/b089b4cd84860aded120ee572fca8b50 to your computer and use it in GitHub Desktop.
Symfon form edit, child object, problem never passes line 32.
<?php
/**
* @Route("/{projectId<\d+>}", methods={"GET", "POST", "PUT"}, name="edit" )
* @Entity("project", expr="repository.find(projectId)")
* @Template
*
* @param string $projectId
* @param EntityManagerInterface $em
* @param Request $request
* @param ProjectHelper $projectHelper
*
* @return array
* @throws \Exception
*/
public function edit(
string $projectId,
EntityManagerInterface $em,
Request $request,
ProjectHelper $projectHelper
) {
$repo = $em->getRepository('App:Project');
$project = $repo->find($projectId);
$object = $repo->getDataObject($project);
$objectClassName =$this->_fqcnToShortCn($project->getObjectFqcn());
$formTypeName = sprintf("App\Form\%sType", $objectClassName);
$form = $this->createForm($formTypeName, $object);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($object);
$em->flush();
$this->addFlash('success', 'Form saved!');
return $this->formSubmitRedirect($form, $request, $project);
}
$templateLocation = 'project_forms/_form.'.$objectClassName.'.html.twig';
if (!$this->get('twig')
->getLoader()
->exists($templateLocation)) {
$templateLocation = 'project_forms/_form.html.twig';
}
$class = get_class($object);
return [
'form' => $form->createView(),
'formTemplate' => $templateLocation,
'project' => $project,
'FormName' => $class::NAME_STR." form",
'navActive' => 'Forms',
'navData' => $projectHelper->navData($project),
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment