У меня есть поле в моем FormType, значение которого я пытаюсь получить на момент его успешной отправки и передать его своему контроллеру. Я устанавливаю значение поля, передавая переменную в форму и используя attr текстового поля, чтобы установить его на соответствующее значение в $options, конечный результат html - <input type = "hidden" id = "listing_editId" name = "listing[editId]" required = "required" value = "1288701182" readonly = "readonly">
ListingType.php
->add('editId', HiddenType::class, [
'required' => true,
'disabled' => false,
'mapped' => true,
'attr' => [
'value' => $options['editId'],
'readonly' => true,
]
])
Я пробовал $form->get('editId');, но он не возвращает значение, я также пробовал $request->get('editId');, но безрезультатно.
ListingController.php
/**
* @Route("/account/listings/create", name = "listing_create")
*/
public function createAction(Request $request)
{
$r = sprintf('%09d', mt_rand(0, 1999999999));
$form = $this->createForm(ListingType::class, null, [
'currency' => $this->getParameter('app.currency'),
'hierarchy_categories' => new Hierarchy($this->getDoctrine()->getRepository('AppBundle:Category'), 'category', 'categories'),
'hierarchy_locations' => new Hierarchy($this->getDoctrine()->getRepository('AppBundle:Location'), 'location', 'locations'),
'editId' => $r,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$listing = $form->getData();
$listing->setUser($this->getUser());
try {
$em = $this->getDoctrine()->getManager();
$em->persist($listing);
// I'd like to be able to get the value of an input field that is "editId" here
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('Listing has been successfully created.'));
} catch (\Exception $e) {
$this->addFlash('danger', $this->get('translator')->trans('An error occurred when creating listing object.'));
}
return $this->redirectToRoute('listing_my');
}
return $this->render('FrontBundle::Listing/create.html.twig', [
'form' => $form->createView(),
'editId' => $r]);
}






Попробуйте $form->get('editId')->getData() или $request->request->get('editId') или $form->getData()['editId']